1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-26 01:39:05 -05:00

Reverse API: SoapySDR output

This commit is contained in:
f4exb 2018-12-26 23:00:31 +01:00
parent 42d69204c6
commit 7d93b67a83
6 changed files with 228 additions and 1 deletions

View File

@ -15,6 +15,8 @@
///////////////////////////////////////////////////////////////////////////////////
#include <QDebug>
#include <QNetworkReply>
#include <QBuffer>
#include "SWGDeviceSettings.h"
#include "SWGSoapySDROutputSettings.h"
@ -47,10 +49,16 @@ SoapySDROutput::SoapySDROutput(DeviceSinkAPI *deviceAPI) :
initTunableElementsSettings(m_settings);
initStreamArgSettings(m_settings);
initDeviceArgSettings(m_settings);
m_networkManager = new QNetworkAccessManager();
connect(m_networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(networkManagerFinished(QNetworkReply*)));
}
SoapySDROutput::~SoapySDROutput()
{
disconnect(m_networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(networkManagerFinished(QNetworkReply*)));
delete m_networkManager;
if (m_running) {
stop();
}
@ -760,6 +768,10 @@ bool SoapySDROutput::handleMessage(const Message& message)
m_deviceAPI->stopGeneration();
}
if (m_settings.m_useReverseAPI) {
webapiReverseSendStartStop(cmd.getStartStop());
}
return true;
}
else if (DeviceSoapySDRShared::MsgReportBuddyChange::match(message))
@ -831,6 +843,7 @@ bool SoapySDROutput::applySettings(const SoapySDROutputSettings& settings, bool
bool globalGainChanged = false;
bool individualGainsChanged = false;
bool deviceArgsChanged = false;
QList<QString> reverseAPIKeys;
SoapySDR::Device *dev = m_deviceShared.m_device;
SoapySDROutputThread *outputThread = findThread();
@ -873,6 +886,7 @@ bool SoapySDROutput::applySettings(const SoapySDROutputSettings& settings, bool
if ((m_settings.m_devSampleRate != settings.m_devSampleRate) || force)
{
reverseAPIKeys.append("devSampleRate");
forwardChangeOwnDSP = true;
forwardChangeToBuddies = true;
@ -904,6 +918,7 @@ bool SoapySDROutput::applySettings(const SoapySDROutputSettings& settings, bool
if ((m_settings.m_log2Interp != settings.m_log2Interp) || force)
{
reverseAPIKeys.append("log2Interp");
forwardChangeOwnDSP = true;
if (outputThread != 0)
@ -913,6 +928,19 @@ bool SoapySDROutput::applySettings(const SoapySDROutputSettings& settings, bool
}
}
if ((m_settings.m_centerFrequency != settings.m_centerFrequency) || force) {
reverseAPIKeys.append("centerFrequency");
}
if ((m_settings.m_transverterMode != settings.m_transverterMode) || force) {
reverseAPIKeys.append("transverterMode");
}
if ((m_settings.m_transverterDeltaFrequency != settings.m_transverterDeltaFrequency) || force) {
reverseAPIKeys.append("transverterDeltaFrequency");
}
if ((m_settings.m_LOppmTenths != settings.m_LOppmTenths) || force) {
reverseAPIKeys.append("LOppmTenths");
}
if ((m_settings.m_centerFrequency != settings.m_centerFrequency)
|| (m_settings.m_transverterMode != settings.m_transverterMode)
|| (m_settings.m_transverterDeltaFrequency != settings.m_transverterDeltaFrequency)
@ -930,6 +958,8 @@ bool SoapySDROutput::applySettings(const SoapySDROutputSettings& settings, bool
if ((m_settings.m_antenna != settings.m_antenna) || force)
{
reverseAPIKeys.append("antenna");
if (dev != 0)
{
try
@ -947,6 +977,7 @@ bool SoapySDROutput::applySettings(const SoapySDROutputSettings& settings, bool
if ((m_settings.m_bandwidth != settings.m_bandwidth) || force)
{
reverseAPIKeys.append("bandwidth");
forwardChangeToBuddies = true;
if (dev != 0)
@ -991,6 +1022,8 @@ bool SoapySDROutput::applySettings(const SoapySDROutputSettings& settings, bool
if ((m_settings.m_globalGain != settings.m_globalGain) || force)
{
reverseAPIKeys.append("globalGain");
if (dev != 0)
{
try
@ -1035,6 +1068,8 @@ bool SoapySDROutput::applySettings(const SoapySDROutputSettings& settings, bool
if ((m_settings.m_autoGain != settings.m_autoGain) || force)
{
reverseAPIKeys.append("autoGain");
if (dev != 0)
{
try
@ -1051,6 +1086,8 @@ bool SoapySDROutput::applySettings(const SoapySDROutputSettings& settings, bool
if ((m_settings.m_autoDCCorrection != settings.m_autoDCCorrection) || force)
{
reverseAPIKeys.append("autoDCCorrection");
if ((dev != 0) && hasDCAutoCorrection())
{
try
@ -1067,6 +1104,8 @@ bool SoapySDROutput::applySettings(const SoapySDROutputSettings& settings, bool
if ((m_settings.m_dcCorrection != settings.m_dcCorrection) || force)
{
reverseAPIKeys.append("dcCorrection");
if ((dev != 0) && hasDCCorrectionValue())
{
try
@ -1083,6 +1122,8 @@ bool SoapySDROutput::applySettings(const SoapySDROutputSettings& settings, bool
if ((m_settings.m_iqCorrection != settings.m_iqCorrection) || force)
{
reverseAPIKeys.append("iqCorrection");
if ((dev != 0) && hasIQCorrectionValue())
{
try
@ -1205,6 +1246,20 @@ bool SoapySDROutput::applySettings(const SoapySDROutputSettings& settings, bool
}
}
if (settings.m_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);
if (fullUpdate || force) {
webapiReverseSendSettings(reverseAPIKeys, settings, true);
} else if (reverseAPIKeys.size() != 0) {
webapiReverseSendSettings(reverseAPIKeys, settings, false);
}
}
m_settings = settings;
if (globalGainChanged || individualGainsChanged)
@ -1724,3 +1779,114 @@ void SoapySDROutput::webapiFormatArgInfo(const SoapySDR::ArgInfo& arg, SWGSDRang
argInfo->getOptionNames()->append(new QString(itOpt.c_str()));
}
}
void SoapySDROutput::webapiReverseSendSettings(QList<QString>& deviceSettingsKeys, const SoapySDROutputSettings& settings, bool force)
{
SWGSDRangel::SWGDeviceSettings *swgDeviceSettings = new SWGSDRangel::SWGDeviceSettings();
swgDeviceSettings->setTx(1);
swgDeviceSettings->setDeviceHwType(new QString("SoapySDR"));
swgDeviceSettings->setSoapySdrOutputSettings(new SWGSDRangel::SWGSoapySDROutputSettings());
swgDeviceSettings->getSoapySdrOutputSettings()->init();
SWGSDRangel::SWGSoapySDROutputSettings *swgSoapySDROutputSettings = swgDeviceSettings->getSoapySdrOutputSettings();
// transfer data that has been modified. When force is on transfer all data except reverse API data
if (deviceSettingsKeys.contains("centerFrequency") || force) {
swgSoapySDROutputSettings->setCenterFrequency(settings.m_centerFrequency);
}
if (deviceSettingsKeys.contains("LOppmTenths") || force) {
swgSoapySDROutputSettings->setLOppmTenths(settings.m_LOppmTenths);
}
if (deviceSettingsKeys.contains("devSampleRate") || force) {
swgSoapySDROutputSettings->setDevSampleRate(settings.m_devSampleRate);
}
if (deviceSettingsKeys.contains("bandwidth") || force) {
swgSoapySDROutputSettings->setBandwidth(settings.m_bandwidth);
}
if (deviceSettingsKeys.contains("log2Interp") || force) {
swgSoapySDROutputSettings->setLog2Interp(settings.m_log2Interp);
}
if (deviceSettingsKeys.contains("transverterDeltaFrequency") || force) {
swgSoapySDROutputSettings->setTransverterDeltaFrequency(settings.m_transverterDeltaFrequency);
}
if (deviceSettingsKeys.contains("transverterMode") || force) {
swgSoapySDROutputSettings->setTransverterMode(settings.m_transverterMode ? 1 : 0);
}
if (deviceSettingsKeys.contains("antenna") || force) {
swgSoapySDROutputSettings->setAntenna(new QString(settings.m_antenna));
}
if (deviceSettingsKeys.contains("globalGain") || force) {
swgSoapySDROutputSettings->setGlobalGain(settings.m_globalGain);
}
if (deviceSettingsKeys.contains("autoGain") || force) {
swgSoapySDROutputSettings->setAutoGain(settings.m_autoGain ? 1 : 0);
}
if (deviceSettingsKeys.contains("autoDCCorrection") || force) {
swgSoapySDROutputSettings->setAutoDcCorrection(settings.m_autoDCCorrection ? 1 : 0);
}
if (deviceSettingsKeys.contains("autoIQCorrection") || force) {
swgSoapySDROutputSettings->setAutoIqCorrection(settings.m_autoIQCorrection ? 1 : 0);
}
if (deviceSettingsKeys.contains("dcCorrection") || force)
{
swgSoapySDROutputSettings->setDcCorrection(new SWGSDRangel::SWGComplex());
swgSoapySDROutputSettings->getDcCorrection()->setReal(settings.m_dcCorrection.real());
swgSoapySDROutputSettings->getDcCorrection()->setImag(settings.m_dcCorrection.imag());
}
if (deviceSettingsKeys.contains("iqCorrection") || force)
{
swgSoapySDROutputSettings->setIqCorrection(new SWGSDRangel::SWGComplex());
swgSoapySDROutputSettings->getIqCorrection()->setReal(settings.m_iqCorrection.real());
swgSoapySDROutputSettings->getIqCorrection()->setImag(settings.m_iqCorrection.imag());
}
QString deviceSettingsURL = QString("http://%1:%2/sdrangel/deviceset/%3/device/settings")
.arg(settings.m_reverseAPIAddress)
.arg(settings.m_reverseAPIPort)
.arg(settings.m_reverseAPIDeviceIndex);
m_networkRequest.setUrl(QUrl(deviceSettingsURL));
m_networkRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
QBuffer *buffer=new QBuffer();
buffer->open((QBuffer::ReadWrite));
buffer->write(swgDeviceSettings->asJson().toUtf8());
buffer->seek(0);
// Always use PATCH to avoid passing reverse API settings
m_networkManager->sendCustomRequest(m_networkRequest, "PATCH", buffer);
delete swgDeviceSettings;
}
void SoapySDROutput::webapiReverseSendStartStop(bool start)
{
QString deviceSettingsURL = QString("http://%1:%2/sdrangel/deviceset/%3/device/run")
.arg(m_settings.m_reverseAPIAddress)
.arg(m_settings.m_reverseAPIPort)
.arg(m_settings.m_reverseAPIDeviceIndex);
m_networkRequest.setUrl(QUrl(deviceSettingsURL));
if (start) {
m_networkManager->sendCustomRequest(m_networkRequest, "POST");
} else {
m_networkManager->sendCustomRequest(m_networkRequest, "DELETE");
}
}
void SoapySDROutput::networkManagerFinished(QNetworkReply *reply)
{
QNetworkReply::NetworkError replyError = reply->error();
if (replyError)
{
qWarning() << "SoapySDROutput::networkManagerFinished:"
<< " error(" << (int) replyError
<< "): " << replyError
<< ": " << reply->errorString();
return;
}
QString answer = reply->readAll();
answer.chop(1); // remove last \n
qDebug("SoapySDROutput::networkManagerFinished: reply:\n%s", answer.toStdString().c_str());
}

View File

@ -17,15 +17,19 @@
#ifndef PLUGINS_SAMPLESINK_SOAPYSDROUTPUT_SOAPYSDROUTPUT_H_
#define PLUGINS_SAMPLESINK_SOAPYSDROUTPUT_SOAPYSDROUTPUT_H_
#include <stdint.h>
#include <QString>
#include <QByteArray>
#include <stdint.h>
#include <QNetworkRequest>
#include "dsp/devicesamplesink.h"
#include "soapysdr/devicesoapysdrshared.h"
#include "soapysdroutputsettings.h"
class QNetworkAccessManager;
class QNetworkReply;
class DeviceSinkAPI;
class SoapySDROutputThread;
@ -42,6 +46,7 @@ namespace SWGSDRangel
}
class SoapySDROutput : public DeviceSampleSink {
Q_OBJECT
public:
class MsgConfigureSoapySDROutput : public Message {
MESSAGE_CLASS_DECLARATION
@ -182,6 +187,8 @@ private:
bool m_running;
SoapySDROutputThread *m_thread;
DeviceSoapySDRShared m_deviceShared;
QNetworkAccessManager *m_networkManager;
QNetworkRequest m_networkRequest;
bool openDevice();
void closeDevice();
@ -196,6 +203,11 @@ private:
QVariant webapiVariantFromArgValue(SWGSDRangel::SWGArgValue *argValue);
void webapiFormatArgValue(const QVariant& v, SWGSDRangel::SWGArgValue *argValue);
void webapiFormatArgInfo(const SoapySDR::ArgInfo& arg, SWGSDRangel::SWGArgInfo *argInfo);
void webapiReverseSendSettings(QList<QString>& deviceSettingsKeys, const SoapySDROutputSettings& settings, bool force);
void webapiReverseSendStartStop(bool start);
private slots:
void networkManagerFinished(QNetworkReply *reply);
};

View File

@ -24,6 +24,8 @@
#include "util/simpleserializer.h"
#include "ui_soapysdroutputgui.h"
#include "gui/glspectrum.h"
#include "gui/crightclickenabler.h"
#include "gui/basicdevicesettingsdialog.h"
#include "soapygui/discreterangegui.h"
#include "soapygui/intervalrangegui.h"
#include "soapygui/stringrangegui.h"
@ -87,6 +89,9 @@ SoapySDROutputGui::SoapySDROutputGui(DeviceUISet *deviceUISet, QWidget* parent)
connect(&m_statusTimer, SIGNAL(timeout()), this, SLOT(updateStatus()));
m_statusTimer.start(500);
CRightClickEnabler *startStopRightClickEnabler = new CRightClickEnabler(ui->startStop);
connect(startStopRightClickEnabler, SIGNAL(rightClick(const QPoint &)), this, SLOT(openDeviceSettingsDialog(const QPoint &)));
displaySettings();
connect(&m_inputMessageQueue, SIGNAL(messageEnqueued()), this, SLOT(handleInputMessages()), Qt::QueuedConnection);
@ -876,3 +881,21 @@ void SoapySDROutputGui::updateStatus()
}
}
void SoapySDROutputGui::openDeviceSettingsDialog(const QPoint& p)
{
BasicDeviceSettingsDialog dialog(this);
dialog.setUseReverseAPI(m_settings.m_useReverseAPI);
dialog.setReverseAPIAddress(m_settings.m_reverseAPIAddress);
dialog.setReverseAPIPort(m_settings.m_reverseAPIPort);
dialog.setReverseAPIDeviceIndex(m_settings.m_reverseAPIDeviceIndex);
dialog.move(p);
dialog.exec();
m_settings.m_useReverseAPI = dialog.useReverseAPI();
m_settings.m_reverseAPIAddress = dialog.getReverseAPIAddress();
m_settings.m_reverseAPIPort = dialog.getReverseAPIPort();
m_settings.m_reverseAPIDeviceIndex = dialog.getReverseAPIDeviceIndex();
sendSettings();
}

View File

@ -138,6 +138,7 @@ private slots:
void on_startStop_toggled(bool checked);
void updateHardware();
void updateStatus();
void openDeviceSettingsDialog(const QPoint& p);
};
#endif /* PLUGINS_SAMPLESINK_SOAPYSDROUTPUT_SOAPYSDROUTPUTGUI_H_ */

View File

@ -42,6 +42,10 @@ void SoapySDROutputSettings::resetToDefaults()
m_autoIQCorrection = false;
m_dcCorrection = std::complex<double>{0,0};
m_iqCorrection = std::complex<double>{0,0};
m_useReverseAPI = false;
m_reverseAPIAddress = "127.0.0.1";
m_reverseAPIPort = 8888;
m_reverseAPIDeviceIndex = 0;
}
QByteArray SoapySDROutputSettings::serialize() const
@ -67,6 +71,10 @@ QByteArray SoapySDROutputSettings::serialize() const
s.writeDouble(20, m_iqCorrection.imag());
s.writeBlob(21, serializeArgumentMap(m_streamArgSettings));
s.writeBlob(22, serializeArgumentMap(m_deviceArgSettings));
s.writeBool(23, m_useReverseAPI);
s.writeString(24, m_reverseAPIAddress);
s.writeU32(25, m_reverseAPIPort);
s.writeU32(26, m_reverseAPIDeviceIndex);
return s.final();
}
@ -85,6 +93,7 @@ bool SoapySDROutputSettings::deserialize(const QByteArray& data)
{
QByteArray blob;
double realval, imagval;
uint32_t uintval;
d.readS32(1, &m_devSampleRate, 1024000);
d.readS32(2, &m_LOppmTenths, 0);
@ -111,6 +120,18 @@ bool SoapySDROutputSettings::deserialize(const QByteArray& data)
deserializeArgumentMap(blob, m_streamArgSettings);
d.readBlob(22, &blob);
deserializeArgumentMap(blob, m_deviceArgSettings);
d.readBool(23, &m_useReverseAPI, false);
d.readString(24, &m_reverseAPIAddress, "127.0.0.1");
d.readU32(25, &uintval, 0);
if ((uintval > 1023) && (uintval < 65535)) {
m_reverseAPIPort = uintval;
} else {
m_reverseAPIPort = 8888;
}
d.readU32(26, &uintval, 0);
m_reverseAPIDeviceIndex = uintval > 99 ? 99 : uintval;
return true;
}

View File

@ -40,6 +40,10 @@ struct SoapySDROutputSettings {
std::complex<double> m_iqCorrection;
QMap<QString, QVariant> m_streamArgSettings;
QMap<QString, QVariant> m_deviceArgSettings;
bool m_useReverseAPI;
QString m_reverseAPIAddress;
uint16_t m_reverseAPIPort;
uint16_t m_reverseAPIDeviceIndex;
SoapySDROutputSettings();
void resetToDefaults();