Skip to content

Commit

Permalink
Local input: Make settings assignments atomic. Part of #1329
Browse files Browse the repository at this point in the history
  • Loading branch information
f4exb committed Oct 23, 2022
1 parent 793bf69 commit 4d8667e
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 36 deletions.
51 changes: 22 additions & 29 deletions plugins/samplesource/localinput/localinput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void LocalInput::destroy()

void LocalInput::init()
{
applySettings(m_settings, true);
applySettings(m_settings, QList<QString>(), true);
}

bool LocalInput::start()
Expand Down Expand Up @@ -106,12 +106,12 @@ bool LocalInput::deserialize(const QByteArray& data)
success = false;
}

MsgConfigureLocalInput* message = MsgConfigureLocalInput::create(m_settings, true);
MsgConfigureLocalInput* message = MsgConfigureLocalInput::create(m_settings, QList<QString>(), true);
m_inputMessageQueue.push(message);

if (m_guiMessageQueue)
{
MsgConfigureLocalInput* messageToGUI = MsgConfigureLocalInput::create(m_settings, true);
MsgConfigureLocalInput* messageToGUI = MsgConfigureLocalInput::create(m_settings, QList<QString>(), true);
m_guiMessageQueue->push(messageToGUI);
}

Expand Down Expand Up @@ -195,7 +195,7 @@ bool LocalInput::handleMessage(const Message& message)
{
qDebug() << "LocalInput::handleMessage:" << message.getIdentifier();
MsgConfigureLocalInput& conf = (MsgConfigureLocalInput&) message;
applySettings(conf.getSettings(), conf.getForce());
applySettings(conf.getSettings(), conf.getSettingsKeys(), conf.getForce());
return true;
}
else
Expand All @@ -204,21 +204,14 @@ bool LocalInput::handleMessage(const Message& message)
}
}

void LocalInput::applySettings(const LocalInputSettings& settings, bool force)
void LocalInput::applySettings(const LocalInputSettings& settings, const QList<QString>& settingsKeys, bool force)
{
qDebug() << "LocalInput::applySettings: force: " << force << settings.getDebugString(settingsKeys, force);
QMutexLocker mutexLocker(&m_mutex);
std::ostringstream os;
QString remoteAddress;
QList<QString> reverseAPIKeys;

if ((m_settings.m_dcBlock != settings.m_dcBlock) || force) {
reverseAPIKeys.append("dcBlock");
}
if ((m_settings.m_iqCorrection != settings.m_iqCorrection) || force) {
reverseAPIKeys.append("iqCorrection");
}

if ((m_settings.m_dcBlock != settings.m_dcBlock) || (m_settings.m_iqCorrection != settings.m_iqCorrection) || force)
if (settingsKeys.contains("dcBlock") || settingsKeys.contains("iqCorrection") || force)
{
m_deviceAPI->configureCorrections(settings.m_dcBlock, settings.m_iqCorrection);
qDebug("LocalInput::applySettings: corrections: DC block: %s IQ imbalance: %s",
Expand All @@ -228,22 +221,22 @@ void LocalInput::applySettings(const LocalInputSettings& settings, bool force)

mutexLocker.unlock();

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);
}

m_settings = settings;
m_remoteAddress = remoteAddress;
if (force) {
m_settings = settings;
} else {
m_settings.applySettings(settingsKeys, settings);
}

qDebug() << "LocalInput::applySettings: "
<< " m_dcBlock: " << m_settings.m_dcBlock
<< " m_iqCorrection: " << m_settings.m_iqCorrection
<< " m_remoteAddress: " << m_remoteAddress;
m_remoteAddress = remoteAddress;
}

int LocalInput::webapiRunGet(
Expand Down Expand Up @@ -295,12 +288,12 @@ int LocalInput::webapiSettingsPutPatch(
LocalInputSettings settings = m_settings;
webapiUpdateDeviceSettings(settings, deviceSettingsKeys, response);

MsgConfigureLocalInput *msg = MsgConfigureLocalInput::create(settings, force);
MsgConfigureLocalInput *msg = MsgConfigureLocalInput::create(settings, deviceSettingsKeys, force);
m_inputMessageQueue.push(msg);

if (m_guiMessageQueue) // forward to GUI if any
{
MsgConfigureLocalInput *msgToGUI = MsgConfigureLocalInput::create(settings, force);
MsgConfigureLocalInput *msgToGUI = MsgConfigureLocalInput::create(settings, deviceSettingsKeys, force);
m_guiMessageQueue->push(msgToGUI);
}

Expand Down Expand Up @@ -367,7 +360,7 @@ void LocalInput::webapiFormatDeviceReport(SWGSDRangel::SWGDeviceReport& response
response.getLocalInputReport()->setSampleRate(m_sampleRate);
}

void LocalInput::webapiReverseSendSettings(QList<QString>& deviceSettingsKeys, const LocalInputSettings& settings, bool force)
void LocalInput::webapiReverseSendSettings(const QList<QString>& deviceSettingsKeys, const LocalInputSettings& settings, bool force)
{
SWGSDRangel::SWGDeviceSettings *swgDeviceSettings = new SWGSDRangel::SWGDeviceSettings();
swgDeviceSettings->setDirection(0); // single Rx
Expand Down
13 changes: 8 additions & 5 deletions plugins/samplesource/localinput/localinput.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,23 @@ class LocalInput : public DeviceSampleSource {

public:
const LocalInputSettings& getSettings() const { return m_settings; }
const QList<QString>& getSettingsKeys() const { return m_settingsKeys; }
bool getForce() const { return m_force; }

static MsgConfigureLocalInput* create(const LocalInputSettings& settings, bool force = false)
static MsgConfigureLocalInput* create(const LocalInputSettings& settings, const QList<QString>& settingsKeys, bool force = false)
{
return new MsgConfigureLocalInput(settings, force);
return new MsgConfigureLocalInput(settings, settingsKeys, force);
}

private:
LocalInputSettings m_settings;
QList<QString> m_settingsKeys;
bool m_force;

MsgConfigureLocalInput(const LocalInputSettings& settings, bool force) :
MsgConfigureLocalInput(const LocalInputSettings& settings, const QList<QString>& settingsKeys, bool force) :
Message(),
m_settings(settings),
m_settingsKeys(settingsKeys),
m_force(force)
{ }
};
Expand Down Expand Up @@ -165,9 +168,9 @@ class LocalInput : public DeviceSampleSource {
QNetworkAccessManager *m_networkManager;
QNetworkRequest m_networkRequest;

void applySettings(const LocalInputSettings& settings, bool force = false);
void applySettings(const LocalInputSettings& settings, const QList<QString>& settingsKeys, bool force = false);
void webapiFormatDeviceReport(SWGSDRangel::SWGDeviceReport& response);
void webapiReverseSendSettings(QList<QString>& deviceSettingsKeys, const LocalInputSettings& settings, bool force);
void webapiReverseSendSettings(const QList<QString>& deviceSettingsKeys, const LocalInputSettings& settings, bool force);
void webapiReverseSendStartStop(bool start);

private slots:
Expand Down
17 changes: 15 additions & 2 deletions plugins/samplesource/localinput/localinputgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,13 @@ bool LocalInputGui::handleMessage(const Message& message)
if (LocalInput::MsgConfigureLocalInput::match(message))
{
const LocalInput::MsgConfigureLocalInput& cfg = (LocalInput::MsgConfigureLocalInput&) message;
m_settings = cfg.getSettings();

if (cfg.getForce()) {
m_settings = cfg.getSettings();
} else {
m_settings.applySettings(cfg.getSettingsKeys(), cfg.getSettings());
}

blockApplySettings(true);
displaySettings();
blockApplySettings(false);
Expand Down Expand Up @@ -255,12 +261,14 @@ void LocalInputGui::sendSettings()
void LocalInputGui::on_dcOffset_toggled(bool checked)
{
m_settings.m_dcBlock = checked;
m_settingsKeys.append("dcBlock");
sendSettings();
}

void LocalInputGui::on_iqImbalance_toggled(bool checked)
{
m_settings.m_iqCorrection = checked;
m_settingsKeys.append("iqCorrection");
sendSettings();
}

Expand All @@ -279,9 +287,10 @@ void LocalInputGui::updateHardware()
{
qDebug() << "LocalInputGui::updateHardware";
LocalInput::MsgConfigureLocalInput* message =
LocalInput::MsgConfigureLocalInput::create(m_settings, m_forceSettings);
LocalInput::MsgConfigureLocalInput::create(m_settings, m_settingsKeys, m_forceSettings);
m_sampleSource->getInputMessageQueue()->push(message);
m_forceSettings = false;
m_settingsKeys.clear();
m_updateTimer.stop();
}
}
Expand Down Expand Up @@ -332,6 +341,10 @@ void LocalInputGui::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();
}
Expand Down
1 change: 1 addition & 0 deletions plugins/samplesource/localinput/localinputgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class LocalInputGui : public DeviceGUI {
Ui::LocalInputGui* ui;

LocalInputSettings m_settings; //!< current settings
QList<QString> m_settingsKeys;
LocalInput* m_sampleSource;
bool m_acquisition;
int m_streamSampleRate; //!< Sample rate of received stream
Expand Down
45 changes: 45 additions & 0 deletions plugins/samplesource/localinput/localinputsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,50 @@ bool LocalInputSettings::deserialize(const QByteArray& data)
}
}

void LocalInputSettings::applySettings(const QStringList& settingsKeys, const LocalInputSettings& settings)
{
if (settingsKeys.contains("dcBlock")) {
m_dcBlock = settings.m_dcBlock;
}
if (settingsKeys.contains("iqCorrection")) {
m_iqCorrection = settings.m_iqCorrection;
}
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 LocalInputSettings::getDebugString(const QStringList& settingsKeys, bool force) const
{
std::ostringstream ostr;

if (settingsKeys.contains("dcBlock") || force) {
ostr << " m_dcBlock: " << m_dcBlock;
}
if (settingsKeys.contains("iqCorrection") || force) {
ostr << " m_iqCorrection: " << m_iqCorrection;
}
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());
}
2 changes: 2 additions & 0 deletions plugins/samplesource/localinput/localinputsettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ struct LocalInputSettings {
void resetToDefaults();
QByteArray serialize() const;
bool deserialize(const QByteArray& data);
void applySettings(const QStringList& settingsKeys, const LocalInputSettings& settings);
QString getDebugString(const QStringList& settingsKeys, bool force=false) const;
};

#endif /* PLUGINS_SAMPLESOURCE_LOCALINPUT_LOCALINPUTSETTINGS_H_ */

0 comments on commit 4d8667e

Please sign in to comment.