Skip to content

Commit

Permalink
KiwiSDR: 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 203a50d commit 30d08b2
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 62 deletions.
21 changes: 19 additions & 2 deletions plugins/samplesource/kiwisdr/kiwisdrgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ void KiwiSDRGui::resetToDefaults()
{
m_settings.resetToDefaults();
displaySettings();
m_forceSettings = true;
sendSettings();
}

Expand Down Expand Up @@ -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();
}

Expand All @@ -159,25 +161,29 @@ 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();
}

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

Expand Down Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();
}
Expand Down
1 change: 1 addition & 0 deletions plugins/samplesource/kiwisdr/kiwisdrgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class KiwiSDRGui : public DeviceGUI {
Ui::KiwiSDRGui* ui;

KiwiSDRSettings m_settings;
QList<QString> m_settingsKeys;
QTimer m_updateTimer;
QTimer m_statusTimer;
bool m_doApplySettings;
Expand Down
85 changes: 32 additions & 53 deletions plugins/samplesource/kiwisdr/kiwisdrinput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void KiwiSDRInput::destroy()

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

bool KiwiSDRInput::start()
Expand All @@ -115,7 +115,7 @@ bool KiwiSDRInput::start()
m_running = true;

mutexLocker.unlock();
applySettings(m_settings, true);
applySettings(m_settings, QList<QString>(), true);

return true;
}
Expand Down Expand Up @@ -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<QString>(), true);
m_inputMessageQueue.push(message);

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

Expand All @@ -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<QString>{"centerFrequency"}, false);
m_inputMessageQueue.push(message);

if (m_guiMessageQueue)
{
MsgConfigureKiwiSDR* messageToGUI = MsgConfigureKiwiSDR::create(settings, false);
MsgConfigureKiwiSDR* messageToGUI = MsgConfigureKiwiSDR::create(settings, QList<QString>{"centerFrequency"}, false);
m_guiMessageQueue->push(messageToGUI);
}
}
Expand All @@ -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");
}

Expand All @@ -227,8 +226,7 @@ bool KiwiSDRInput::handleMessage(const Message& message)

if (cmd.getStartStop())
{
if (m_deviceAPI->initDeviceEngine())
{
if (m_deviceAPI->initDeviceEngine()) {
m_deviceAPI->startDeviceEngine();
}
}
Expand Down Expand Up @@ -258,68 +256,49 @@ int KiwiSDRInput::getStatus() const
}
}

bool KiwiSDRInput::applySettings(const KiwiSDRSettings& settings, bool force)
bool KiwiSDRInput::applySettings(const KiwiSDRSettings& settings, const QList<QString>& 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;

QList<QString> reverseAPIKeys;

if (m_settings.m_serverAddress != settings.m_serverAddress || force)
qDebug() << "KiwiSDRInput::applySettings: force: "<< force << settings.getDebugString(settingsKeys, 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(
getSampleRate(), settings.m_centerFrequency);
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;
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -460,7 +439,7 @@ void KiwiSDRInput::webapiFormatDeviceReport(SWGSDRangel::SWGDeviceReport& respon
response.getKiwiSdrReport()->setStatus(getStatus());
}

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

public:
const KiwiSDRSettings& getSettings() const { return m_settings; }
const QList<QString>& 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<QString>& settingsKeys, bool force)
{
return new MsgConfigureKiwiSDR(settings, force);
return new MsgConfigureKiwiSDR(settings, settingsKeys, force);
}

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

MsgConfigureKiwiSDR(const KiwiSDRSettings& settings, bool force) :
MsgConfigureKiwiSDR(const KiwiSDRSettings& settings, const QList<QString>& settingsKeys, bool force) :
Message(),
m_settings(settings),
m_settingsKeys(settingsKeys),
m_force(force)
{ }
};
Expand Down Expand Up @@ -162,9 +165,9 @@ class KiwiSDRInput : public DeviceSampleSource {
QNetworkRequest m_networkRequest;

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

signals:
Expand Down
64 changes: 62 additions & 2 deletions plugins/samplesource/kiwisdr/kiwisdrsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
2 changes: 2 additions & 0 deletions plugins/samplesource/kiwisdr/kiwisdrsettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_ */

0 comments on commit 30d08b2

Please sign in to comment.