From 475fe5d0802bd5f1161932862948c92470203a0d Mon Sep 17 00:00:00 2001 From: f4exb Date: Sat, 29 Oct 2022 03:21:45 +0200 Subject: [PATCH] SigMF file input: Make settings assignments atomic. Part of #1329 --- .../sigmffileinput/sigmffileinput.cpp | 55 ++++++++--------- .../sigmffileinput/sigmffileinput.h | 14 +++-- .../sigmffileinput/sigmffileinputgui.cpp | 29 +++++++-- .../sigmffileinput/sigmffileinputgui.h | 2 + .../sigmffileinput/sigmffileinputsettings.cpp | 60 +++++++++++++++++++ .../sigmffileinput/sigmffileinputsettings.h | 2 + 6 files changed, 121 insertions(+), 41 deletions(-) diff --git a/plugins/samplesource/sigmffileinput/sigmffileinput.cpp b/plugins/samplesource/sigmffileinput/sigmffileinput.cpp index 63f8af446..4c445782b 100644 --- a/plugins/samplesource/sigmffileinput/sigmffileinput.cpp +++ b/plugins/samplesource/sigmffileinput/sigmffileinput.cpp @@ -533,12 +533,12 @@ bool SigMFFileInput::deserialize(const QByteArray& data) success = false; } - MsgConfigureSigMFFileInput* message = MsgConfigureSigMFFileInput::create(m_settings, true); + MsgConfigureSigMFFileInput* message = MsgConfigureSigMFFileInput::create(m_settings, QList(), true); m_inputMessageQueue.push(message); if (getMessageQueueToGUI()) { - MsgConfigureSigMFFileInput* messageToGUI = MsgConfigureSigMFFileInput::create(m_settings, true); + MsgConfigureSigMFFileInput* messageToGUI = MsgConfigureSigMFFileInput::create(m_settings, QList(), true); getMessageQueueToGUI()->push(messageToGUI); } @@ -565,12 +565,12 @@ void SigMFFileInput::setCenterFrequency(qint64 centerFrequency) SigMFFileInputSettings settings = m_settings; m_centerFrequency = centerFrequency; - MsgConfigureSigMFFileInput* message = MsgConfigureSigMFFileInput::create(m_settings, false); + MsgConfigureSigMFFileInput* message = MsgConfigureSigMFFileInput::create(m_settings, QList{"centerFrequency"}, false); m_inputMessageQueue.push(message); if (getMessageQueueToGUI()) { - MsgConfigureSigMFFileInput* messageToGUI = MsgConfigureSigMFFileInput::create(m_settings, false); + MsgConfigureSigMFFileInput* messageToGUI = MsgConfigureSigMFFileInput::create(m_settings, QList{"centerFrequency"}, false); getMessageQueueToGUI()->push(messageToGUI); } } @@ -585,8 +585,7 @@ bool SigMFFileInput::handleMessage(const Message& message) if (MsgConfigureSigMFFileInput::match(message)) { MsgConfigureSigMFFileInput& conf = (MsgConfigureSigMFFileInput&) message; - SigMFFileInputSettings settings = conf.getSettings(); - applySettings(settings); + applySettings(conf.getSettings(), conf.getSettingsKeys(), conf.getForce()); return true; } else if (MsgConfigureTrackIndex::match(message)) @@ -816,14 +815,12 @@ bool SigMFFileInput::handleMessage(const Message& message) } } -bool SigMFFileInput::applySettings(const SigMFFileInputSettings& settings, bool force) +bool SigMFFileInput::applySettings(const SigMFFileInputSettings& settings, const QList& settingsKeys, bool force) { - QList reverseAPIKeys; + qDebug() << "SigMFFileInput::applySettings: force: " << force << settings.getDebugString(settingsKeys, force); - if ((m_settings.m_accelerationFactor != settings.m_accelerationFactor) || force) + if (settingsKeys.contains("accelerationFactor") || force) { - reverseAPIKeys.append("accelerationFactor"); - if (m_fileInputWorker) { QMutexLocker mutexLocker(&m_mutex); @@ -836,29 +833,25 @@ bool SigMFFileInput::applySettings(const SigMFFileInputSettings& settings, bool } } - if ((m_settings.m_trackLoop != settings.m_trackLoop)) { - reverseAPIKeys.append("trackLoop"); - } - if ((m_settings.m_trackLoop != settings.m_fullLoop)) { - reverseAPIKeys.append("fullLoop"); - } - - if ((m_settings.m_fileName != settings.m_fileName)) - { - reverseAPIKeys.append("fileName"); + if (settingsKeys.contains("fileName")) { openFileStreams(settings.m_fileName); } - if (settings.m_useReverseAPI) + if (settingsKeys.contains("useReverseAPI")) { - 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; } @@ -883,12 +876,12 @@ int SigMFFileInput::webapiSettingsPutPatch( SigMFFileInputSettings settings = m_settings; webapiUpdateDeviceSettings(settings, deviceSettingsKeys, response); - MsgConfigureSigMFFileInput *msg = MsgConfigureSigMFFileInput::create(settings, force); + MsgConfigureSigMFFileInput *msg = MsgConfigureSigMFFileInput::create(settings, deviceSettingsKeys, force); m_inputMessageQueue.push(msg); if (m_guiMessageQueue) // forward to GUI if any { - MsgConfigureSigMFFileInput *msgToGUI = MsgConfigureSigMFFileInput::create(settings, force); + MsgConfigureSigMFFileInput *msgToGUI = MsgConfigureSigMFFileInput::create(settings, deviceSettingsKeys, force); m_guiMessageQueue->push(msgToGUI); } @@ -1130,7 +1123,7 @@ void SigMFFileInput::webapiFormatDeviceReport(SWGSDRangel::SWGDeviceReport& resp } } -void SigMFFileInput::webapiReverseSendSettings(QList& deviceSettingsKeys, const SigMFFileInputSettings& settings, bool force) +void SigMFFileInput::webapiReverseSendSettings(const QList& deviceSettingsKeys, const SigMFFileInputSettings& settings, bool force) { SWGSDRangel::SWGDeviceSettings *swgDeviceSettings = new SWGSDRangel::SWGDeviceSettings(); swgDeviceSettings->setDirection(0); // single Rx diff --git a/plugins/samplesource/sigmffileinput/sigmffileinput.h b/plugins/samplesource/sigmffileinput/sigmffileinput.h index d886fb447..95e3235a6 100644 --- a/plugins/samplesource/sigmffileinput/sigmffileinput.h +++ b/plugins/samplesource/sigmffileinput/sigmffileinput.h @@ -50,20 +50,22 @@ public: public: const SigMFFileInputSettings& getSettings() const { return m_settings; } + const QList& getSettingsKeys() const { return m_settingsKeys; } bool getForce() const { return m_force; } - static MsgConfigureSigMFFileInput* create(const SigMFFileInputSettings& settings, bool force) - { - return new MsgConfigureSigMFFileInput(settings, force); + static MsgConfigureSigMFFileInput* create(const SigMFFileInputSettings& settings, const QList& settingsKeys, bool force) { + return new MsgConfigureSigMFFileInput(settings, settingsKeys, force); } private: SigMFFileInputSettings m_settings; + QList m_settingsKeys; bool m_force; - MsgConfigureSigMFFileInput(const SigMFFileInputSettings& settings, bool force) : + MsgConfigureSigMFFileInput(const SigMFFileInputSettings& settings, const QList& settingsKeys, bool force) : Message(), m_settings(settings), + m_settingsKeys(settingsKeys), m_force(force) { } }; @@ -489,9 +491,9 @@ private: void seekFileStream(uint64_t sampleIndex); void seekTrackMillis(int seekMillis); void seekFileMillis(int seekMillis); - bool applySettings(const SigMFFileInputSettings& settings, bool force = false); + bool applySettings(const SigMFFileInputSettings& settings, const QList& settingsKeys, bool force = false); void webapiFormatDeviceReport(SWGSDRangel::SWGDeviceReport& response); - void webapiReverseSendSettings(QList& deviceSettingsKeys, const SigMFFileInputSettings& settings, bool force); + void webapiReverseSendSettings(const QList& deviceSettingsKeys, const SigMFFileInputSettings& settings, bool force); void webapiReverseSendStartStop(bool start); private slots: diff --git a/plugins/samplesource/sigmffileinput/sigmffileinputgui.cpp b/plugins/samplesource/sigmffileinput/sigmffileinputgui.cpp index 60992c5f8..c94be303c 100644 --- a/plugins/samplesource/sigmffileinput/sigmffileinputgui.cpp +++ b/plugins/samplesource/sigmffileinput/sigmffileinputgui.cpp @@ -44,6 +44,7 @@ SigMFFileInputGUI::SigMFFileInputGUI(DeviceUISet *deviceUISet, QWidget* parent) DeviceGUI(parent), ui(new Ui::SigMFFileInputGUI), m_settings(), + m_forceSettings(false), m_currentTrackIndex(0), m_doApplySettings(true), m_sampleSource(0), @@ -112,6 +113,7 @@ void SigMFFileInputGUI::resetToDefaults() { m_settings.resetToDefaults(); displaySettings(); + m_forceSettings = true; sendSettings(); } @@ -122,11 +124,14 @@ QByteArray SigMFFileInputGUI::serialize() const bool SigMFFileInputGUI::deserialize(const QByteArray& data) { - if(m_settings.deserialize(data)) { + if(m_settings.deserialize(data)) + { displaySettings(); + m_forceSettings = true; sendSettings(); return true; - } else { + } else + { resetToDefaults(); return false; } @@ -163,7 +168,13 @@ bool SigMFFileInputGUI::handleMessage(const Message& message) if (SigMFFileInput::MsgConfigureSigMFFileInput::match(message)) { const SigMFFileInput::MsgConfigureSigMFFileInput& cfg = (SigMFFileInput::MsgConfigureSigMFFileInput&) message; - m_settings = cfg.getSettings(); + + if (cfg.getForce()) { + m_settings = cfg.getSettings(); + } else { + m_settings.applySettings(cfg.getSettingsKeys(), cfg.getSettings()); + } + displaySettings(); return true; } @@ -377,8 +388,10 @@ void SigMFFileInputGUI::sendSettings() { if (m_doApplySettings) { - SigMFFileInput::MsgConfigureSigMFFileInput *message = SigMFFileInput::MsgConfigureSigMFFileInput::create(m_settings, false); + SigMFFileInput::MsgConfigureSigMFFileInput *message = SigMFFileInput::MsgConfigureSigMFFileInput::create(m_settings, m_settingsKeys, m_forceSettings); m_sampleSource->getInputMessageQueue()->push(message); + m_forceSettings = false; + m_settingsKeys.clear(); } } @@ -439,6 +452,7 @@ void SigMFFileInputGUI::on_fullNavTimeSlider_valueChanged(int value) void SigMFFileInputGUI::on_playTrackLoop_toggled(bool checked) { m_settings.m_trackLoop = checked; + m_settingsKeys.append("trackLoop"); sendSettings(); } @@ -459,6 +473,7 @@ void SigMFFileInputGUI::on_playTrack_toggled(bool checked) void SigMFFileInputGUI::on_playFullLoop_toggled(bool checked) { m_settings.m_fullLoop = checked; + m_settingsKeys.append("fullLoop"); sendSettings(); } @@ -479,6 +494,7 @@ void SigMFFileInputGUI::on_playFull_toggled(bool checked) void SigMFFileInputGUI::on_acceleration_currentIndexChanged(int index) { m_settings.m_accelerationFactor = SigMFFileInputSettings::getAccelerationValue(index); + m_settingsKeys.append("accelerationFactor"); sendSettings(); } @@ -542,6 +558,7 @@ void SigMFFileInputGUI::configureFileName() if (recordType == FileRecordInterface::RecordTypeSigMF) { m_settings.m_fileName = fileBase; + m_settingsKeys.append("fileName"); sendSettings(); } } @@ -684,6 +701,10 @@ void SigMFFileInputGUI::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/sigmffileinput/sigmffileinputgui.h b/plugins/samplesource/sigmffileinput/sigmffileinputgui.h index b2a72848e..37870d76c 100644 --- a/plugins/samplesource/sigmffileinput/sigmffileinputgui.h +++ b/plugins/samplesource/sigmffileinput/sigmffileinputgui.h @@ -51,6 +51,8 @@ private: Ui::SigMFFileInputGUI* ui; SigMFFileInputSettings m_settings; + QList m_settingsKeys; + bool m_forceSettings; int m_currentTrackIndex; bool m_doApplySettings; QTimer m_statusTimer; diff --git a/plugins/samplesource/sigmffileinput/sigmffileinputsettings.cpp b/plugins/samplesource/sigmffileinput/sigmffileinputsettings.cpp index 5abdce315..88762fd26 100644 --- a/plugins/samplesource/sigmffileinput/sigmffileinputsettings.cpp +++ b/plugins/samplesource/sigmffileinput/sigmffileinputsettings.cpp @@ -92,6 +92,66 @@ bool SigMFFileInputSettings::deserialize(const QByteArray& data) } } +void SigMFFileInputSettings::applySettings(const QStringList& settingsKeys, const SigMFFileInputSettings& settings) +{ + if (settingsKeys.contains("fileName")) { + m_fileName = settings.m_fileName; + } + if (settingsKeys.contains("accelerationFactor")) { + m_accelerationFactor = settings.m_accelerationFactor; + } + if (settingsKeys.contains("trackLoop")) { + m_trackLoop = settings.m_trackLoop; + } + if (settingsKeys.contains("fullLoop")) { + m_fullLoop = settings.m_fullLoop; + } + 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 SigMFFileInputSettings::getDebugString(const QStringList& settingsKeys, bool force) const +{ + std::ostringstream ostr; + + if (settingsKeys.contains("m_fileName") || force) { + ostr << " m_fileName: " << m_fileName.toStdString(); + } + if (settingsKeys.contains("accelerationFactor") || force) { + ostr << " m_accelerationFactor: " << m_accelerationFactor; + } + if (settingsKeys.contains("trackLoop") || force) { + ostr << " m_trackLoop: " << m_trackLoop; + } + if (settingsKeys.contains("fullLoop") || force) { + ostr << " m_fullLoop: " << m_fullLoop; + } + 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()); +} + int SigMFFileInputSettings::getAccelerationIndex(int accelerationValue) { if (accelerationValue <= 1) { diff --git a/plugins/samplesource/sigmffileinput/sigmffileinputsettings.h b/plugins/samplesource/sigmffileinput/sigmffileinputsettings.h index 9e365494f..675af6637 100644 --- a/plugins/samplesource/sigmffileinput/sigmffileinputsettings.h +++ b/plugins/samplesource/sigmffileinput/sigmffileinputsettings.h @@ -39,6 +39,8 @@ struct SigMFFileInputSettings { void resetToDefaults(); QByteArray serialize() const; bool deserialize(const QByteArray& data); + void applySettings(const QStringList& settingsKeys, const SigMFFileInputSettings& settings); + QString getDebugString(const QStringList& settingsKeys, bool force=false) const; static int getAccelerationIndex(int averaging); static int getAccelerationValue(int averagingIndex); static int bitsToBytes(int bits);