From 30d08b2911c4584e69d06dc420ffefcba8e0d81e Mon Sep 17 00:00:00 2001 From: f4exb Date: Sun, 23 Oct 2022 22:02:47 +0200 Subject: [PATCH] KiwiSDR: Make settings assignments atomic. Part of #1329 --- plugins/samplesource/kiwisdr/kiwisdrgui.cpp | 21 ++++- plugins/samplesource/kiwisdr/kiwisdrgui.h | 1 + plugins/samplesource/kiwisdr/kiwisdrinput.cpp | 83 +++++++------------ plugins/samplesource/kiwisdr/kiwisdrinput.h | 13 +-- .../samplesource/kiwisdr/kiwisdrsettings.cpp | 64 +++++++++++++- .../samplesource/kiwisdr/kiwisdrsettings.h | 2 + 6 files changed, 123 insertions(+), 61 deletions(-) diff --git a/plugins/samplesource/kiwisdr/kiwisdrgui.cpp b/plugins/samplesource/kiwisdr/kiwisdrgui.cpp index e36a93136..ce90bcfc5 100644 --- a/plugins/samplesource/kiwisdr/kiwisdrgui.cpp +++ b/plugins/samplesource/kiwisdr/kiwisdrgui.cpp @@ -101,6 +101,7 @@ void KiwiSDRGui::resetToDefaults() { m_settings.resetToDefaults(); displaySettings(); + m_forceSettings = true; sendSettings(); } @@ -140,6 +141,7 @@ void KiwiSDRGui::on_startStop_toggled(bool checked) void KiwiSDRGui::on_centerFrequency_changed(quint64 value) { m_settings.m_centerFrequency = value * 1000; + m_settingsKeys.append("centerFrequency"); sendSettings(); } @@ -159,18 +161,21 @@ void KiwiSDRGui::on_serverAddressApplyButton_clicked() m_settings.m_serverAddress = serverAddress; } + m_settingsKeys.append("serverAddress"); sendSettings(); } void KiwiSDRGui::on_dcBlock_toggled(bool checked) { m_settings.m_dcBlock = checked; + m_settingsKeys.append("dcBlock"); sendSettings(); } void KiwiSDRGui::on_agc_toggled(bool checked) { m_settings.m_useAGC = checked; + m_settingsKeys.append("useAGC"); sendSettings(); } @@ -178,6 +183,7 @@ void KiwiSDRGui::on_gain_valueChanged(int value) { m_settings.m_gain = value; ui->gainText->setText(QString::number(m_settings.m_gain) + " dB"); + m_settingsKeys.append("gain"); sendSettings(); } @@ -206,9 +212,10 @@ void KiwiSDRGui::updateHardware() { if (m_doApplySettings) { - KiwiSDRInput::MsgConfigureKiwiSDR* message = KiwiSDRInput::MsgConfigureKiwiSDR::create(m_settings, m_forceSettings); + KiwiSDRInput::MsgConfigureKiwiSDR* message = KiwiSDRInput::MsgConfigureKiwiSDR::create(m_settings, m_settingsKeys, m_forceSettings); m_sampleSource->getInputMessageQueue()->push(message); m_forceSettings = false; + m_settingsKeys.clear(); m_updateTimer.stop(); } } @@ -248,7 +255,13 @@ bool KiwiSDRGui::handleMessage(const Message& message) { qDebug("KiwiSDRGui::handleMessage: MsgConfigureKiwiSDR"); const KiwiSDRInput::MsgConfigureKiwiSDR& cfg = (KiwiSDRInput::MsgConfigureKiwiSDR&) message; - m_settings = cfg.getSettings(); + + if (cfg.getForce()) { + m_settings = cfg.getSettings(); + } else { + m_settings.applySettings(cfg.getSettingsKeys(), cfg.getSettings()); + } + displaySettings(); return true; } @@ -330,6 +343,10 @@ void KiwiSDRGui::openDeviceSettingsDialog(const QPoint& p) m_settings.m_reverseAPIAddress = dialog.getReverseAPIAddress(); m_settings.m_reverseAPIPort = dialog.getReverseAPIPort(); m_settings.m_reverseAPIDeviceIndex = dialog.getReverseAPIDeviceIndex(); + m_settingsKeys.append("useReverseAPI"); + m_settingsKeys.append("reverseAPIAddress"); + m_settingsKeys.append("reverseAPIPort"); + m_settingsKeys.append("reverseAPIDeviceIndex"); sendSettings(); } diff --git a/plugins/samplesource/kiwisdr/kiwisdrgui.h b/plugins/samplesource/kiwisdr/kiwisdrgui.h index 385426c7c..7e39465fb 100644 --- a/plugins/samplesource/kiwisdr/kiwisdrgui.h +++ b/plugins/samplesource/kiwisdr/kiwisdrgui.h @@ -54,6 +54,7 @@ private: Ui::KiwiSDRGui* ui; KiwiSDRSettings m_settings; + QList m_settingsKeys; QTimer m_updateTimer; QTimer m_statusTimer; bool m_doApplySettings; diff --git a/plugins/samplesource/kiwisdr/kiwisdrinput.cpp b/plugins/samplesource/kiwisdr/kiwisdrinput.cpp index 2bd8a82ea..d64f8df38 100644 --- a/plugins/samplesource/kiwisdr/kiwisdrinput.cpp +++ b/plugins/samplesource/kiwisdr/kiwisdrinput.cpp @@ -88,7 +88,7 @@ void KiwiSDRInput::destroy() void KiwiSDRInput::init() { - applySettings(m_settings, true); + applySettings(m_settings, QList(), true); } bool KiwiSDRInput::start() @@ -115,7 +115,7 @@ bool KiwiSDRInput::start() m_running = true; mutexLocker.unlock(); - applySettings(m_settings, true); + applySettings(m_settings, QList(), true); return true; } @@ -155,12 +155,12 @@ bool KiwiSDRInput::deserialize(const QByteArray& data) success = false; } - MsgConfigureKiwiSDR* message = MsgConfigureKiwiSDR::create(m_settings, true); + MsgConfigureKiwiSDR* message = MsgConfigureKiwiSDR::create(m_settings, QList(), true); m_inputMessageQueue.push(message); if (m_guiMessageQueue) { - MsgConfigureKiwiSDR* messageToGUI = MsgConfigureKiwiSDR::create(m_settings, true); + MsgConfigureKiwiSDR* messageToGUI = MsgConfigureKiwiSDR::create(m_settings, QList(), true); m_guiMessageQueue->push(messageToGUI); } @@ -187,12 +187,12 @@ void KiwiSDRInput::setCenterFrequency(qint64 centerFrequency) KiwiSDRSettings settings = m_settings; settings.m_centerFrequency = centerFrequency; - MsgConfigureKiwiSDR* message = MsgConfigureKiwiSDR::create(settings, false); + MsgConfigureKiwiSDR* message = MsgConfigureKiwiSDR::create(settings, QList{"centerFrequency"}, false); m_inputMessageQueue.push(message); if (m_guiMessageQueue) { - MsgConfigureKiwiSDR* messageToGUI = MsgConfigureKiwiSDR::create(settings, false); + MsgConfigureKiwiSDR* messageToGUI = MsgConfigureKiwiSDR::create(settings, QList{"centerFrequency"}, false); m_guiMessageQueue->push(messageToGUI); } } @@ -211,10 +211,9 @@ bool KiwiSDRInput::handleMessage(const Message& message) MsgConfigureKiwiSDR& conf = (MsgConfigureKiwiSDR&) message; qDebug() << "KiwiSDRInput::handleMessage: MsgConfigureKiwiSDR"; - bool success = applySettings(conf.getSettings(), conf.getForce()); + bool success = applySettings(conf.getSettings(), conf.getSettingsKeys(), conf.getForce()); - if (!success) - { + if (!success) { qDebug("KiwiSDRInput::handleMessage: config error"); } @@ -227,8 +226,7 @@ bool KiwiSDRInput::handleMessage(const Message& message) if (cmd.getStartStop()) { - if (m_deviceAPI->initDeviceEngine()) - { + if (m_deviceAPI->initDeviceEngine()) { m_deviceAPI->startDeviceEngine(); } } @@ -258,50 +256,27 @@ int KiwiSDRInput::getStatus() const } } -bool KiwiSDRInput::applySettings(const KiwiSDRSettings& settings, bool force) +bool KiwiSDRInput::applySettings(const KiwiSDRSettings& settings, const QList& settingsKeys, bool force) { - qDebug() << "KiwiSDRInput::applySettings: " - << " m_serverAddress: " << settings.m_serverAddress - << " m_centerFrequency: " << settings.m_centerFrequency - << " m_gain: " << settings.m_gain - << " m_useAGC: " << settings.m_useAGC - << " m_useAGC: " << settings.m_useAGC - << " m_useReverseAPI: " << settings.m_useReverseAPI - << " m_reverseAPIAddress: " << settings.m_reverseAPIAddress - << " m_reverseAPIPort: " << settings.m_reverseAPIPort - << " m_reverseAPIDeviceIndex: " << settings.m_reverseAPIDeviceIndex; + qDebug() << "KiwiSDRInput::applySettings: force: "<< force << settings.getDebugString(settingsKeys, force); - QList reverseAPIKeys; - - if (m_settings.m_serverAddress != settings.m_serverAddress || force) + if (settingsKeys.contains("serverAddress") || force) { - reverseAPIKeys.append("serverAddress"); emit setWorkerServerAddress(settings.m_serverAddress); } - if (m_settings.m_gain != settings.m_gain || force) { - reverseAPIKeys.append("gain"); - } - if (m_settings.m_useAGC != settings.m_useAGC || force) { - reverseAPIKeys.append("useAGC"); - } - - if (m_settings.m_gain != settings.m_gain || - m_settings.m_useAGC != settings.m_useAGC || force) + if (settingsKeys.contains("gain") || + settingsKeys.contains("useAGC") || force) { emit setWorkerGain(settings.m_gain, settings.m_useAGC); } - if (m_settings.m_dcBlock != settings.m_dcBlock) - { - reverseAPIKeys.append("dcBlock"); + if (settingsKeys.contains("dcBlock")) { m_deviceAPI->configureCorrections(settings.m_dcBlock, false); } - if (m_settings.m_centerFrequency != settings.m_centerFrequency || force) + if (settingsKeys.contains("centerFrequency") || force) { - reverseAPIKeys.append("centerFrequency"); - emit setWorkerCenterFrequency(settings.m_centerFrequency); DSPSignalNotification *notif = new DSPSignalNotification( @@ -309,17 +284,21 @@ bool KiwiSDRInput::applySettings(const KiwiSDRSettings& settings, bool force) m_deviceAPI->getDeviceEngineInputMessageQueue()->push(notif); } - if (settings.m_useReverseAPI) + if (settingsKeys.contains("useReverseAPI")) { - qDebug("KiwiSDRInput::applySettings: call webapiReverseSendSettings"); - bool fullUpdate = ((m_settings.m_useReverseAPI != settings.m_useReverseAPI) && settings.m_useReverseAPI) || - (m_settings.m_reverseAPIAddress != settings.m_reverseAPIAddress) || - (m_settings.m_reverseAPIPort != settings.m_reverseAPIPort) || - (m_settings.m_reverseAPIDeviceIndex != settings.m_reverseAPIDeviceIndex); - webapiReverseSendSettings(reverseAPIKeys, settings, fullUpdate || force); + bool fullUpdate = (settingsKeys.contains("useReverseAPI") && settings.m_useReverseAPI) || + settingsKeys.contains("reverseAPIAddress") || + settingsKeys.contains("reverseAPIPort") || + settingsKeys.contains("reverseAPIDeviceIndex"); + webapiReverseSendSettings(settingsKeys, settings, fullUpdate || force); + } + + if (force) { + m_settings = settings; + } else { + m_settings.applySettings(settingsKeys, settings); } - m_settings = settings; return true; } @@ -372,12 +351,12 @@ int KiwiSDRInput::webapiSettingsPutPatch( KiwiSDRSettings settings = m_settings; webapiUpdateDeviceSettings(settings, deviceSettingsKeys, response); - MsgConfigureKiwiSDR *msg = MsgConfigureKiwiSDR::create(settings, force); + MsgConfigureKiwiSDR *msg = MsgConfigureKiwiSDR::create(settings, deviceSettingsKeys, force); m_inputMessageQueue.push(msg); if (m_guiMessageQueue) // forward to GUI if any { - MsgConfigureKiwiSDR *msgToGUI = MsgConfigureKiwiSDR::create(settings, force); + MsgConfigureKiwiSDR *msgToGUI = MsgConfigureKiwiSDR::create(settings, deviceSettingsKeys, force); m_guiMessageQueue->push(msgToGUI); } @@ -460,7 +439,7 @@ void KiwiSDRInput::webapiFormatDeviceReport(SWGSDRangel::SWGDeviceReport& respon response.getKiwiSdrReport()->setStatus(getStatus()); } -void KiwiSDRInput::webapiReverseSendSettings(QList& deviceSettingsKeys, const KiwiSDRSettings& settings, bool force) +void KiwiSDRInput::webapiReverseSendSettings(const QList& deviceSettingsKeys, const KiwiSDRSettings& settings, bool force) { SWGSDRangel::SWGDeviceSettings *swgDeviceSettings = new SWGSDRangel::SWGDeviceSettings(); swgDeviceSettings->setDirection(0); // single Rx diff --git a/plugins/samplesource/kiwisdr/kiwisdrinput.h b/plugins/samplesource/kiwisdr/kiwisdrinput.h index 6fff5b266..09b9a3af8 100644 --- a/plugins/samplesource/kiwisdr/kiwisdrinput.h +++ b/plugins/samplesource/kiwisdr/kiwisdrinput.h @@ -41,20 +41,23 @@ public: public: const KiwiSDRSettings& getSettings() const { return m_settings; } + const QList& getSettingsKeys() const { return m_settingsKeys; } bool getForce() const { return m_force; } - static MsgConfigureKiwiSDR* create(const KiwiSDRSettings& settings, bool force) + static MsgConfigureKiwiSDR* create(const KiwiSDRSettings& settings, const QList& settingsKeys, bool force) { - return new MsgConfigureKiwiSDR(settings, force); + return new MsgConfigureKiwiSDR(settings, settingsKeys, force); } private: KiwiSDRSettings m_settings; + QList m_settingsKeys; bool m_force; - MsgConfigureKiwiSDR(const KiwiSDRSettings& settings, bool force) : + MsgConfigureKiwiSDR(const KiwiSDRSettings& settings, const QList& settingsKeys, bool force) : Message(), m_settings(settings), + m_settingsKeys(settingsKeys), m_force(force) { } }; @@ -162,9 +165,9 @@ private: QNetworkRequest m_networkRequest; int getStatus() const; - bool applySettings(const KiwiSDRSettings& settings, bool force); + bool applySettings(const KiwiSDRSettings& settings, const QList& settingsKeys, bool force); void webapiFormatDeviceReport(SWGSDRangel::SWGDeviceReport& response); - void webapiReverseSendSettings(QList& deviceSettingsKeys, const KiwiSDRSettings& settings, bool force); + void webapiReverseSendSettings(const QList& deviceSettingsKeys, const KiwiSDRSettings& settings, bool force); void webapiReverseSendStartStop(bool start); signals: diff --git a/plugins/samplesource/kiwisdr/kiwisdrsettings.cpp b/plugins/samplesource/kiwisdr/kiwisdrsettings.cpp index f894288c7..d4a381981 100644 --- a/plugins/samplesource/kiwisdr/kiwisdrsettings.cpp +++ b/plugins/samplesource/kiwisdr/kiwisdrsettings.cpp @@ -97,8 +97,68 @@ bool KiwiSDRSettings::deserialize(const QByteArray& data) } } +void KiwiSDRSettings::applySettings(const QStringList& settingsKeys, const KiwiSDRSettings& settings) +{ + if (settingsKeys.contains("centerFrequency")) { + m_centerFrequency = settings.m_centerFrequency; + } + if (settingsKeys.contains("gain")) { + m_gain = settings.m_gain; + } + if (settingsKeys.contains("useAGC")) { + m_useAGC = settings.m_useAGC; + } + if (settingsKeys.contains("dcBlock")) { + m_dcBlock = settings.m_dcBlock; + } + if (settingsKeys.contains("serverAddress")) { + m_serverAddress = settings.m_serverAddress; + } + if (settingsKeys.contains("useReverseAPI")) { + m_useReverseAPI = settings.m_useReverseAPI; + } + if (settingsKeys.contains("reverseAPIAddress")) { + m_reverseAPIAddress = settings.m_reverseAPIAddress; + } + if (settingsKeys.contains("reverseAPIPort")) { + m_reverseAPIPort = settings.m_reverseAPIPort; + } + if (settingsKeys.contains("reverseAPIDeviceIndex")) { + m_reverseAPIDeviceIndex = settings.m_reverseAPIDeviceIndex; + } +} +QString KiwiSDRSettings::getDebugString(const QStringList& settingsKeys, bool force) const +{ + std::ostringstream ostr; + if (settingsKeys.contains("centerFrequency") || force) { + ostr << " m_centerFrequency: " << m_centerFrequency; + } + if (settingsKeys.contains("gain") || force) { + ostr << " m_gain: " << m_gain; + } + if (settingsKeys.contains("useAGC") || force) { + ostr << " m_useAGC: " << m_useAGC; + } + if (settingsKeys.contains("dcBlock") || force) { + ostr << " m_dcBlock: " << m_dcBlock; + } + if (settingsKeys.contains("serverAddress") || force) { + ostr << " m_serverAddress: " << m_serverAddress.toStdString(); + } + if (settingsKeys.contains("useReverseAPI") || force) { + ostr << " m_useReverseAPI: " << m_useReverseAPI; + } + if (settingsKeys.contains("reverseAPIAddress") || force) { + ostr << " m_reverseAPIAddress: " << m_reverseAPIAddress.toStdString(); + } + if (settingsKeys.contains("reverseAPIPort") || force) { + ostr << " m_reverseAPIPort: " << m_reverseAPIPort; + } + if (settingsKeys.contains("reverseAPIDeviceIndex") || force) { + ostr << " m_reverseAPIDeviceIndex: " << m_reverseAPIDeviceIndex; + } - - + return QString(ostr.str().c_str()); +} diff --git a/plugins/samplesource/kiwisdr/kiwisdrsettings.h b/plugins/samplesource/kiwisdr/kiwisdrsettings.h index 23b09f312..23cd7e60b 100644 --- a/plugins/samplesource/kiwisdr/kiwisdrsettings.h +++ b/plugins/samplesource/kiwisdr/kiwisdrsettings.h @@ -39,6 +39,8 @@ struct KiwiSDRSettings { void resetToDefaults(); QByteArray serialize() const; bool deserialize(const QByteArray& data); + void applySettings(const QStringList& settingsKeys, const KiwiSDRSettings& settings); + QString getDebugString(const QStringList& settingsKeys, bool force=false) const; }; #endif /* _KIWISDR_KIWISDRSETTINGS_H_ */