Reverse API: SoapySDR input

This commit is contained in:
f4exb 2018-12-26 18:15:43 +01:00
parent c6e057b143
commit bc4c9aeba6
6 changed files with 249 additions and 1 deletions

View File

@ -15,6 +15,8 @@
///////////////////////////////////////////////////////////////////////////////////
#include <QDebug>
#include <QNetworkReply>
#include <QBuffer>
#include "util/simpleserializer.h"
@ -54,10 +56,16 @@ SoapySDRInput::SoapySDRInput(DeviceSourceAPI *deviceAPI) :
m_fileSink = new FileRecord(QString("test_%1.sdriq").arg(m_deviceAPI->getDeviceUID()));
m_deviceAPI->addSink(m_fileSink);
m_networkManager = new QNetworkAccessManager();
connect(m_networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(networkManagerFinished(QNetworkReply*)));
}
SoapySDRInput::~SoapySDRInput()
{
disconnect(m_networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(networkManagerFinished(QNetworkReply*)));
delete m_networkManager;
if (m_running) {
stop();
}
@ -811,6 +819,10 @@ bool SoapySDRInput::handleMessage(const Message& message)
m_deviceAPI->stopAcquisition();
}
if (m_settings.m_useReverseAPI) {
webapiReverseSendStartStop(cmd.getStartStop());
}
return true;
}
else if (DeviceSoapySDRShared::MsgReportBuddyChange::match(message))
@ -888,6 +900,7 @@ bool SoapySDRInput::applySettings(const SoapySDRInputSettings& settings, bool fo
bool globalGainChanged = false;
bool individualGainsChanged = false;
bool deviceArgsChanged = false;
QList<QString> reverseAPIKeys;
SoapySDR::Device *dev = m_deviceShared.m_device;
SoapySDRInputThread *inputThread = findThread();
@ -896,6 +909,13 @@ bool SoapySDRInput::applySettings(const SoapySDRInputSettings& settings, bool fo
xlatedDeviceCenterFrequency -= settings.m_transverterMode ? settings.m_transverterDeltaFrequency : 0;
xlatedDeviceCenterFrequency = xlatedDeviceCenterFrequency < 0 ? 0 : xlatedDeviceCenterFrequency;
if ((m_settings.m_softDCCorrection != settings.m_softDCCorrection) || force) {
reverseAPIKeys.append("softDCCorrection");
}
if ((m_settings.m_softIQCorrection != settings.m_softIQCorrection) || force) {
reverseAPIKeys.append("softIQCorrection");
}
if ((m_settings.m_softDCCorrection != settings.m_softDCCorrection) ||
(m_settings.m_softIQCorrection != settings.m_softIQCorrection) || force)
{
@ -904,6 +924,7 @@ bool SoapySDRInput::applySettings(const SoapySDRInputSettings& settings, bool fo
if ((m_settings.m_devSampleRate != settings.m_devSampleRate) || force)
{
reverseAPIKeys.append("devSampleRate");
forwardChangeOwnDSP = true;
forwardChangeToBuddies = true;
@ -935,6 +956,8 @@ bool SoapySDRInput::applySettings(const SoapySDRInputSettings& settings, bool fo
if ((m_settings.m_fcPos != settings.m_fcPos) || force)
{
reverseAPIKeys.append("fcPos");
if (inputThread != 0)
{
inputThread->setFcPos(requestedChannel, (int) settings.m_fcPos);
@ -944,6 +967,7 @@ bool SoapySDRInput::applySettings(const SoapySDRInputSettings& settings, bool fo
if ((m_settings.m_log2Decim != settings.m_log2Decim) || force)
{
reverseAPIKeys.append("log2Decim");
forwardChangeOwnDSP = true;
SoapySDRInputThread *inputThread = findThread();
@ -954,6 +978,19 @@ bool SoapySDRInput::applySettings(const SoapySDRInputSettings& settings, bool fo
}
}
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)
@ -979,6 +1016,8 @@ bool SoapySDRInput::applySettings(const SoapySDRInputSettings& settings, bool fo
if ((m_settings.m_antenna != settings.m_antenna) || force)
{
reverseAPIKeys.append("antenna");
if (dev != 0)
{
try
@ -996,6 +1035,7 @@ bool SoapySDRInput::applySettings(const SoapySDRInputSettings& settings, bool fo
if ((m_settings.m_bandwidth != settings.m_bandwidth) || force)
{
reverseAPIKeys.append("bandwidth");
forwardChangeToBuddies = true;
if (dev != 0)
@ -1040,6 +1080,8 @@ bool SoapySDRInput::applySettings(const SoapySDRInputSettings& settings, bool fo
if ((m_settings.m_globalGain != settings.m_globalGain) || force)
{
reverseAPIKeys.append("globalGain");
if (dev != 0)
{
try
@ -1084,6 +1126,8 @@ bool SoapySDRInput::applySettings(const SoapySDRInputSettings& settings, bool fo
if ((m_settings.m_autoGain != settings.m_autoGain) || force)
{
reverseAPIKeys.append("autoGain");
if (dev != 0)
{
try
@ -1100,6 +1144,8 @@ bool SoapySDRInput::applySettings(const SoapySDRInputSettings& settings, bool fo
if ((m_settings.m_autoDCCorrection != settings.m_autoDCCorrection) || force)
{
reverseAPIKeys.append("autoDCCorrection");
if ((dev != 0) && hasDCAutoCorrection())
{
try
@ -1116,6 +1162,8 @@ bool SoapySDRInput::applySettings(const SoapySDRInputSettings& settings, bool fo
if ((m_settings.m_dcCorrection != settings.m_dcCorrection) || force)
{
reverseAPIKeys.append("dcCorrection");
if ((dev != 0) && hasDCCorrectionValue())
{
try
@ -1132,6 +1180,8 @@ bool SoapySDRInput::applySettings(const SoapySDRInputSettings& settings, bool fo
if ((m_settings.m_iqCorrection != settings.m_iqCorrection) || force)
{
reverseAPIKeys.append("iqCorrection");
if ((dev != 0) && hasIQCorrectionValue())
{
try
@ -1255,6 +1305,20 @@ bool SoapySDRInput::applySettings(const SoapySDRInputSettings& settings, bool fo
}
}
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)
@ -1799,3 +1863,126 @@ void SoapySDRInput::webapiFormatArgInfo(const SoapySDR::ArgInfo& arg, SWGSDRange
argInfo->getOptionNames()->append(new QString(itOpt.c_str()));
}
}
void SoapySDRInput::webapiReverseSendSettings(QList<QString>& deviceSettingsKeys, const SoapySDRInputSettings& settings, bool force)
{
SWGSDRangel::SWGDeviceSettings *swgDeviceSettings = new SWGSDRangel::SWGDeviceSettings();
swgDeviceSettings->setTx(0);
swgDeviceSettings->setDeviceHwType(new QString("SoapySDR"));
swgDeviceSettings->setSoapySdrInputSettings(new SWGSDRangel::SWGSoapySDRInputSettings());
swgDeviceSettings->getSoapySdrInputSettings()->init();
SWGSDRangel::SWGSoapySDRInputSettings *swgSoapySDRInputSettings = swgDeviceSettings->getSoapySdrInputSettings();
// transfer data that has been modified. When force is on transfer all data except reverse API data
if (deviceSettingsKeys.contains("centerFrequency") || force) {
swgSoapySDRInputSettings->setCenterFrequency(settings.m_centerFrequency);
}
if (deviceSettingsKeys.contains("LOppmTenths") || force) {
swgSoapySDRInputSettings->setLOppmTenths(settings.m_LOppmTenths);
}
if (deviceSettingsKeys.contains("devSampleRate") || force) {
swgSoapySDRInputSettings->setDevSampleRate(settings.m_devSampleRate);
}
if (deviceSettingsKeys.contains("bandwidth") || force) {
swgSoapySDRInputSettings->setBandwidth(settings.m_bandwidth);
}
if (deviceSettingsKeys.contains("log2Decim") || force) {
swgSoapySDRInputSettings->setLog2Decim(settings.m_log2Decim);
}
if (deviceSettingsKeys.contains("fcPos") || force) {
swgSoapySDRInputSettings->setFcPos((int) settings.m_fcPos);
}
if (deviceSettingsKeys.contains("softDCCorrection") || force) {
swgSoapySDRInputSettings->setSoftDcCorrection(settings.m_softDCCorrection ? 1 : 0);
}
if (deviceSettingsKeys.contains("softIQCorrection") || force) {
swgSoapySDRInputSettings->setSoftIqCorrection(settings.m_softIQCorrection ? 1 : 0);
}
if (deviceSettingsKeys.contains("transverterDeltaFrequency") || force) {
swgSoapySDRInputSettings->setTransverterDeltaFrequency(settings.m_transverterDeltaFrequency);
}
if (deviceSettingsKeys.contains("transverterMode") || force) {
swgSoapySDRInputSettings->setTransverterMode(settings.m_transverterMode ? 1 : 0);
}
if (deviceSettingsKeys.contains("fileRecordName") || force) {
swgSoapySDRInputSettings->setFileRecordName(new QString(settings.m_fileRecordName));
}
if (deviceSettingsKeys.contains("antenna") || force) {
swgSoapySDRInputSettings->setAntenna(new QString(settings.m_antenna));
}
if (deviceSettingsKeys.contains("globalGain") || force) {
swgSoapySDRInputSettings->setGlobalGain(settings.m_globalGain);
}
if (deviceSettingsKeys.contains("autoGain") || force) {
swgSoapySDRInputSettings->setAutoGain(settings.m_autoGain ? 1 : 0);
}
if (deviceSettingsKeys.contains("autoDCCorrection") || force) {
swgSoapySDRInputSettings->setAutoDcCorrection(settings.m_autoDCCorrection ? 1 : 0);
}
if (deviceSettingsKeys.contains("autoIQCorrection") || force) {
swgSoapySDRInputSettings->setAutoIqCorrection(settings.m_autoIQCorrection ? 1 : 0);
}
if (deviceSettingsKeys.contains("dcCorrection") || force)
{
swgSoapySDRInputSettings->setDcCorrection(new SWGSDRangel::SWGComplex());
swgSoapySDRInputSettings->getDcCorrection()->setReal(settings.m_dcCorrection.real());
swgSoapySDRInputSettings->getDcCorrection()->setImag(settings.m_dcCorrection.imag());
}
if (deviceSettingsKeys.contains("iqCorrection") || force)
{
swgSoapySDRInputSettings->setIqCorrection(new SWGSDRangel::SWGComplex());
swgSoapySDRInputSettings->getIqCorrection()->setReal(settings.m_iqCorrection.real());
swgSoapySDRInputSettings->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 SoapySDRInput::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 SoapySDRInput::networkManagerFinished(QNetworkReply *reply)
{
QNetworkReply::NetworkError replyError = reply->error();
if (replyError)
{
qWarning() << "SoapySDRInput::networkManagerFinished:"
<< " error(" << (int) replyError
<< "): " << replyError
<< ": " << reply->errorString();
return;
}
QString answer = reply->readAll();
answer.chop(1); // remove last \n
qDebug("SoapySDRInput::networkManagerFinished: reply:\n%s", answer.toStdString().c_str());
}

View File

@ -17,15 +17,19 @@
#ifndef PLUGINS_SAMPLESOURCE_SOAPYSDRINPUT_SOAPYSDRINPUT_H_
#define PLUGINS_SAMPLESOURCE_SOAPYSDRINPUT_SOAPYSDRINPUT_H_
#include <stdint.h>
#include <QString>
#include <QByteArray>
#include <stdint.h>
#include <QNetworkRequest>
#include "soapysdr/devicesoapysdrshared.h"
#include "dsp/devicesamplesource.h"
#include "soapysdrinputsettings.h"
class QNetworkAccessManager;
class QNetworkReply;
class DeviceSourceAPI;
class SoapySDRInputThread;
class FileRecord;
@ -44,6 +48,7 @@ namespace SWGSDRangel
class SoapySDRInput : public DeviceSampleSource
{
Q_OBJECT
public:
class MsgConfigureSoapySDRInput : public Message {
MESSAGE_CLASS_DECLARATION
@ -205,6 +210,8 @@ private:
SoapySDRInputThread *m_thread;
DeviceSoapySDRShared m_deviceShared;
FileRecord *m_fileSink; //!< File sink to record device I/Q output
QNetworkAccessManager *m_networkManager;
QNetworkRequest m_networkRequest;
bool openDevice();
void closeDevice();
@ -219,6 +226,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 SoapySDRInputSettings& settings, bool force);
void webapiReverseSendStartStop(bool start);
private slots:
void networkManagerFinished(QNetworkReply *reply);
};

View File

@ -23,6 +23,8 @@
#include "device/deviceuiset.h"
#include "util/simpleserializer.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"
@ -88,6 +90,9 @@ SoapySDRInputGui::SoapySDRInputGui(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);
@ -926,3 +931,21 @@ void SoapySDRInputGui::updateStatus()
}
}
void SoapySDRInputGui::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

@ -139,6 +139,7 @@ private slots:
void on_record_toggled(bool checked);
void updateHardware();
void updateStatus();
void openDeviceSettingsDialog(const QPoint& p);
};

View File

@ -45,6 +45,10 @@ void SoapySDRInputSettings::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 SoapySDRInputSettings::serialize() const
@ -73,6 +77,10 @@ QByteArray SoapySDRInputSettings::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();
}
@ -90,6 +98,7 @@ bool SoapySDRInputSettings::deserialize(const QByteArray& data)
if (d.getVersion() == 1)
{
int intval;
uint32_t uintval;
QByteArray blob;
double realval, imagval;
@ -122,6 +131,18 @@ bool SoapySDRInputSettings::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

@ -51,6 +51,10 @@ struct SoapySDRInputSettings {
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;
SoapySDRInputSettings();
void resetToDefaults();