1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-27 02:09:14 -05:00
sdrangel/plugins/channelrx/noisefigure/noisefigure.cpp

936 lines
34 KiB
C++
Raw Normal View History

2021-06-09 11:17:59 -04:00
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2015-2018 Edouard Griffiths, F4EXB. //
// Copyright (C) 2021 Jon Beniston, M7RCE //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation as version 3 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License V3 for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
///////////////////////////////////////////////////////////////////////////////////
#include <boost/version.hpp>
2021-06-09 11:17:59 -04:00
#include <boost/math/interpolators/barycentric_rational.hpp>
#include "noisefigure.h"
#include <QTimer>
#include <QDebug>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QBuffer>
#include <QProcess>
#include <complex.h>
#include "SWGChannelSettings.h"
#include "SWGWorkspaceInfo.h"
2021-06-09 11:17:59 -04:00
#include "SWGChannelReport.h"
#include "dsp/dspengine.h"
#include "dsp/dspcommands.h"
#include "device/deviceapi.h"
#include "feature/feature.h"
#include "util/db.h"
#include "util/interpolation.h"
2021-06-09 11:17:59 -04:00
#include "channel/channelwebapiutils.h"
#include "maincore.h"
#if BOOST_VERSION < 107700
using namespace boost::math;
#else
using namespace boost::math::interpolators;
#endif
2021-06-09 11:17:59 -04:00
MESSAGE_CLASS_DEFINITION(NoiseFigure::MsgConfigureNoiseFigure, Message)
MESSAGE_CLASS_DEFINITION(NoiseFigure::MsgPowerMeasurement, Message)
MESSAGE_CLASS_DEFINITION(NoiseFigure::MsgNFMeasurement, Message)
MESSAGE_CLASS_DEFINITION(NoiseFigure::MsgStartStop, Message)
MESSAGE_CLASS_DEFINITION(NoiseFigure::MsgFinished, Message)
const char * const NoiseFigure::m_channelIdURI = "sdrangel.channel.noisefigure";
const char * const NoiseFigure::m_channelId = "NoiseFigure";
NoiseFigure::NoiseFigure(DeviceAPI *deviceAPI) :
ChannelAPI(m_channelIdURI, ChannelAPI::StreamSingleSink),
m_deviceAPI(deviceAPI),
m_basebandSampleRate(0),
m_state(IDLE)
{
setObjectName(m_channelId);
m_basebandSink = new NoiseFigureBaseband(this);
m_basebandSink->setMessageQueueToChannel(getInputMessageQueue());
m_basebandSink->setChannel(this);
m_basebandSink->moveToThread(&m_thread);
applySettings(m_settings, true);
m_deviceAPI->addChannelSink(this);
m_deviceAPI->addChannelSinkAPI(this);
m_networkManager = new QNetworkAccessManager();
QObject::connect(
m_networkManager,
&QNetworkAccessManager::finished,
this,
&NoiseFigure::networkManagerFinished
);
QObject::connect(
this,
&ChannelAPI::indexInDeviceSetChanged,
this,
&NoiseFigure::handleIndexInDeviceSetChanged
);
2021-06-09 11:17:59 -04:00
}
NoiseFigure::~NoiseFigure()
{
qDebug("NoiseFigure::~NoiseFigure");
QObject::disconnect(
m_networkManager,
&QNetworkAccessManager::finished,
this,
&NoiseFigure::networkManagerFinished
);
2021-06-09 11:17:59 -04:00
delete m_networkManager;
m_deviceAPI->removeChannelSinkAPI(this);
m_deviceAPI->removeChannelSink(this);
if (m_basebandSink->isRunning()) {
stop();
}
delete m_basebandSink;
}
void NoiseFigure::setDeviceAPI(DeviceAPI *deviceAPI)
{
if (deviceAPI != m_deviceAPI)
{
m_deviceAPI->removeChannelSinkAPI(this);
m_deviceAPI->removeChannelSink(this);
m_deviceAPI = deviceAPI;
m_deviceAPI->addChannelSink(this);
m_deviceAPI->addChannelSinkAPI(this);
}
}
2021-06-09 11:17:59 -04:00
uint32_t NoiseFigure::getNumberOfDeviceStreams() const
{
return m_deviceAPI->getNbSourceStreams();
}
void NoiseFigure::setCenterFrequency(qint64 frequency)
{
NoiseFigureSettings settings = m_settings;
settings.m_inputFrequencyOffset = frequency;
applySettings(settings, false);
if (m_guiMessageQueue) // forward to GUI if any
{
MsgConfigureNoiseFigure *msgToGUI = MsgConfigureNoiseFigure::create(settings, false);
m_guiMessageQueue->push(msgToGUI);
}
}
2021-06-09 11:17:59 -04:00
void NoiseFigure::feed(const SampleVector::const_iterator& begin, const SampleVector::const_iterator& end, bool firstOfBurst)
{
(void) firstOfBurst;
m_basebandSink->feed(begin, end);
}
void NoiseFigure::start()
{
qDebug("NoiseFigure::start");
m_basebandSink->reset();
m_basebandSink->startWork();
m_thread.start();
DSPSignalNotification *dspMsg = new DSPSignalNotification(m_basebandSampleRate, m_centerFrequency);
m_basebandSink->getInputMessageQueue()->push(dspMsg);
NoiseFigureBaseband::MsgConfigureNoiseFigureBaseband *msg = NoiseFigureBaseband::MsgConfigureNoiseFigureBaseband::create(m_settings, true);
m_basebandSink->getInputMessageQueue()->push(msg);
}
void NoiseFigure::stop()
{
qDebug("NoiseFigure::stop");
m_basebandSink->stopWork();
m_thread.quit();
m_thread.wait();
}
bool NoiseFigure::openVISADevice()
2021-06-09 11:17:59 -04:00
{
m_visa.openDefault();
m_session = m_visa.open(m_settings.m_visaDevice);
return m_session != 0;
2021-06-09 11:17:59 -04:00
}
void NoiseFigure::closeVISADevice()
{
m_visa.close(m_session);
m_visa.closeDefault();
}
void NoiseFigure::processVISA(QStringList commands)
{
if (m_session > 0)
{
for (int i = 0; i < commands.size(); i++)
{
QString command = commands[i].trimmed();
if (!command.isEmpty() && !command.startsWith("#")) // Allow # to comment out lines
{
qDebug() << "VISA ->: " << command;
QByteArray bytes = QString("%1\n").arg(command).toLatin1();
char *cmd = bytes.data();
m_visa.viPrintf(m_session, cmd);
if (command.endsWith("?"))
{
char buf[1024] = "";
2021-06-09 12:14:56 -04:00
char format[] = "%t";
m_visa.viScanf(m_session, format, buf);
2021-06-09 11:17:59 -04:00
qDebug() << "VISA <-: " << buf;
}
}
}
}
}
// Calculate ENR at specified frequency
double NoiseFigure::calcENR(double frequency)
{
double enr;
2021-06-09 11:17:59 -04:00
int size = m_settings.m_enr.size();
if (size >= 2)
{
std::vector<double> x(size);
std::vector<double> y(size);
for (int i = 0; i < size; i++)
{
x[i] = m_settings.m_enr[i]->m_frequency;
y[i] = m_settings.m_enr[i]->m_enr;
}
if (m_settings.m_interpolation == NoiseFigureSettings::LINEAR)
{
enr = Interpolation::linear(x.begin(), x.end(), y.begin(), frequency);
}
else
{
int order = size - 1;
barycentric_rational<double> interpolant(std::move(x), std::move(y), order);
enr = interpolant(frequency);
}
2021-06-09 11:17:59 -04:00
}
else if (size == 1)
{
enr = m_settings.m_enr[0]->m_enr;
2021-06-09 11:17:59 -04:00
}
else
{
// Shouldn't get here
enr = 0.0;
2021-06-09 11:17:59 -04:00
}
qDebug() << "ENR at " << frequency << " interpolated to " << enr;
return enr;
2021-06-09 11:17:59 -04:00
}
// FSM for running measurements over multiple frequencies
void NoiseFigure::nextState()
{
double scaleFactor = m_settings.m_setting == "centerFrequency" ? 1e6 : 1.0;
2021-06-09 11:17:59 -04:00
switch (m_state)
{
case IDLE:
if (m_settings.m_enr.size() < 1)
{
if (getMessageQueueToGUI()) {
getMessageQueueToGUI()->push(MsgFinished::create("ENRs has not been specified"));
}
return;
}
m_step = 0;
if (m_settings.m_sweepSpec == NoiseFigureSettings::LIST)
2021-06-09 11:17:59 -04:00
{
// Create list of sweep values from string
QRegularExpression separator("[( |,|\t|)]");
QStringList valueStrings = m_settings.m_sweepList.trimmed().split(separator);
m_values.clear();
for (int i = 0; i < valueStrings.size(); i++)
2021-06-09 11:17:59 -04:00
{
QString valueString = valueStrings[i].trimmed();
if (!valueString.isEmpty())
2021-06-09 11:17:59 -04:00
{
bool ok;
double value = valueString.toDouble(&ok);
2021-06-09 11:17:59 -04:00
if (ok) {
m_values.append(value * scaleFactor);
2021-06-09 11:17:59 -04:00
} else {
qDebug() << "NoiseFigure::nextState: Invalid value: " << valueString;
2021-06-09 11:17:59 -04:00
}
}
}
if (m_values.size() == 0)
2021-06-09 11:17:59 -04:00
{
qDebug() << "NoiseFigure::nextState: Sweep list is empty";
2021-06-09 11:17:59 -04:00
if (getMessageQueueToGUI()) {
getMessageQueueToGUI()->push(MsgFinished::create("Sweep list is empty"));
2021-06-09 11:17:59 -04:00
}
return;
}
// Set start value and number of values to step through
m_sweepValue = m_values[0];
m_steps = m_values.size();
2021-06-09 11:17:59 -04:00
}
else
{
if (m_settings.m_stopValue < m_settings.m_startValue)
2021-06-09 11:17:59 -04:00
{
if (getMessageQueueToGUI()) {
getMessageQueueToGUI()->push(MsgFinished::create("Stop value must be greater or equal to start value"));
2021-06-09 11:17:59 -04:00
}
return;
}
// Set start value and number of values to step through
m_sweepValue = m_settings.m_startValue * scaleFactor;
if (m_settings.m_sweepSpec == NoiseFigureSettings::RANGE) {
2021-06-09 11:17:59 -04:00
m_steps = m_settings.m_steps;
} else {
m_steps = (m_settings.m_stopValue - m_settings.m_startValue) / m_settings.m_step + 1;
2021-06-09 11:17:59 -04:00
}
}
m_state = SET_SWEEP_VALUE;
QTimer::singleShot(0, this, &NoiseFigure::nextState);
2021-06-09 11:17:59 -04:00
break;
case SET_SWEEP_VALUE:
// Set device setting that is being swept
if (ChannelWebAPIUtils::patchDeviceSetting(getDeviceSetIndex(), m_settings.m_setting, m_sweepValue))
2021-06-09 11:17:59 -04:00
{
qDebug() << "NoiseFigure::nextState: Set " << m_settings.m_setting << " to " << m_sweepValue;
2021-06-09 11:17:59 -04:00
m_state = POWER_ON;
QTimer::singleShot(100, this, &NoiseFigure::nextState);
2021-06-09 11:17:59 -04:00
} else
{
qDebug() << "NoiseFigure::nextState: Unable to set " << m_settings.m_setting << " to " << m_sweepValue;
2021-06-09 11:17:59 -04:00
}
break;
case POWER_ON:
// Power on noise source
powerOn();
QTimer::singleShot(m_settings.m_powerDelay * 1000.0, this, &NoiseFigure::nextState);
2021-06-09 11:17:59 -04:00
m_state = MEASURE_ON;
break;
case MEASURE_ON:
// Start measurement of power when noise source is on
qDebug() << "NoiseFigure::nextState: Starting on measurement";
m_basebandSink->startMeasurement();
break;
case POWER_OFF:
// Power off noise source
powerOff();
QTimer::singleShot(m_settings.m_powerDelay * 1000.0, this, &NoiseFigure::nextState);
2021-06-09 11:17:59 -04:00
m_state = MEASURE_OFF;
break;
case MEASURE_OFF:
// Start measurement of power when noise source is off
qDebug() << "NoiseFigure::nextState: Starting off measurement";
m_basebandSink->startMeasurement();
break;
case COMPLETE:
// Calculate noise figure and temperature using Y-factor method
double t = 290.0;
double k = 1.38064852e-23;
double bw = 1;
2021-06-09 11:17:59 -04:00
double y = m_onPower - m_offPower;
double enr = calcENR(m_centerFrequency/1e6);
2021-06-09 11:17:59 -04:00
double nf = 10.0*log10(pow(10.0, enr/10.0)/(pow(10.0, y/10.0)-1.0));
double temp = t*(pow(10.0, nf/10.0)-1.0);
double floor = 10.0*log10(1000.0*k*t) + nf + 10*log10(bw);
2021-06-09 11:17:59 -04:00
// Send result to GUI
if (getMessageQueueToGUI())
{
MsgNFMeasurement *msg = MsgNFMeasurement::create(m_sweepValue/scaleFactor, nf, temp, y, enr, floor);
2021-06-09 11:17:59 -04:00
getMessageQueueToGUI()->push(msg);
}
m_step++;
if (m_step >= m_steps)
{
// All values swept
2021-06-09 11:17:59 -04:00
closeVISADevice();
m_state = IDLE;
if (getMessageQueueToGUI()) {
getMessageQueueToGUI()->push(MsgFinished::create());
}
}
else
{
// Move to next value in sweep
if (m_settings.m_sweepSpec == NoiseFigureSettings::LIST) {
m_sweepValue = m_values[m_step];
} else if (m_settings.m_sweepSpec == NoiseFigureSettings::RANGE) {
m_sweepValue += scaleFactor * (m_settings.m_stopValue - m_settings.m_startValue) / (m_settings.m_steps - 1);
2021-06-09 11:17:59 -04:00
} else {
m_sweepValue += m_settings.m_step * scaleFactor;
2021-06-09 11:17:59 -04:00
}
m_state = SET_SWEEP_VALUE;
QTimer::singleShot(0, this, &NoiseFigure::nextState);
2021-06-09 11:17:59 -04:00
}
break;
}
}
void NoiseFigure::powerOn()
{
QString command = m_settings.m_powerOnCommand.trimmed();
if (!command.isEmpty())
{
2021-12-27 17:30:18 -05:00
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
QStringList allArgs = command.split(" ", Qt::SkipEmptyParts);
2021-12-27 17:30:18 -05:00
#else
QStringList allArgs = command.split(" ", QString::SkipEmptyParts);
#endif
QString program = allArgs[0];
allArgs.pop_front();
qDebug() << "NoiseFigure::powerOn(): Executing" << program << allArgs;
int exitCode = QProcess::execute(program, allArgs);
if (exitCode) {
qWarning() << "NoiseFigure::powerOn():" << program << "exited with code" << exitCode;
}
2021-06-09 11:17:59 -04:00
}
QStringList commands = m_settings.m_powerOnSCPI.split("\n");
processVISA(commands);
}
void NoiseFigure::powerOff()
{
QStringList commands = m_settings.m_powerOffSCPI.split("\n");
processVISA(commands);
QString command = m_settings.m_powerOffCommand.trimmed();
if (!command.isEmpty())
{
2021-12-27 17:30:18 -05:00
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
QStringList allArgs = command.split(" ", Qt::SkipEmptyParts);
2021-12-27 17:30:18 -05:00
#else
QStringList allArgs = command.split(" ", QString::SkipEmptyParts);
#endif
QString program = allArgs[0];
allArgs.pop_front();
qDebug() << "NoiseFigure::powerOff(): Executing" << program << allArgs;
int exitCode = QProcess::execute(program, allArgs);
if (exitCode) {
qWarning() << "NoiseFigure::powerOff():" << program << "exited with code" << exitCode;
}
2021-06-09 11:17:59 -04:00
}
}
bool NoiseFigure::handleMessage(const Message& cmd)
{
if (MsgConfigureNoiseFigure::match(cmd))
{
MsgConfigureNoiseFigure& cfg = (MsgConfigureNoiseFigure&) cmd;
qDebug() << "NoiseFigure::handleMessage: MsgConfigureNoiseFigure";
applySettings(cfg.getSettings(), cfg.getForce());
return true;
}
else if (DSPSignalNotification::match(cmd))
{
DSPSignalNotification& notif = (DSPSignalNotification&) cmd;
m_basebandSampleRate = notif.getSampleRate();
m_centerFrequency = notif.getCenterFrequency();
// Forward to the sink
DSPSignalNotification* rep = new DSPSignalNotification(notif); // make a copy
qDebug() << "NoiseFigure::handleMessage: DSPSignalNotification";
m_basebandSink->getInputMessageQueue()->push(rep);
// Forward to GUI if any
if (m_guiMessageQueue) {
m_guiMessageQueue->push(new DSPSignalNotification(notif));
2021-06-09 11:17:59 -04:00
}
return true;
}
else if (MsgPowerMeasurement::match(cmd))
{
MsgPowerMeasurement& report = (MsgPowerMeasurement&)cmd;
if (m_state == MEASURE_ON)
{
m_onPower = report.getPower();
m_state = POWER_OFF;
nextState();
}
else if (m_state == MEASURE_OFF)
{
m_offPower = report.getPower();
m_state = COMPLETE;
nextState();
}
return true;
}
else if (MsgStartStop::match(cmd))
{
if (m_state == IDLE)
{
if (!m_settings.m_visaDevice.isEmpty())
{
if (openVISADevice()) {
QTimer::singleShot(0, this, &NoiseFigure::nextState);
} else if (getMessageQueueToGUI()) {
getMessageQueueToGUI()->push(MsgFinished::create(QString("Failed to open VISA device %1").arg(m_settings.m_visaDevice)));
}
}
else
{
QTimer::singleShot(0, this, &NoiseFigure::nextState);
}
2021-06-09 11:17:59 -04:00
}
else
{
// Set maximum step so test stops after current measurement
m_step = m_steps;
}
return true;
}
else
{
return false;
}
}
void NoiseFigure::applySettings(const NoiseFigureSettings& settings, bool force)
{
qDebug() << "NoiseFigure::applySettings:"
<< " m_inputFrequencyOffset: " << settings.m_inputFrequencyOffset
<< " m_fftSize: " << settings.m_fftSize
<< " m_fftCount: " << settings.m_fftCount
<< " m_sweepSpec: " << settings.m_sweepSpec
<< " m_startValue: " << settings.m_startValue
<< " m_stopValue: " << settings.m_stopValue
2021-06-09 11:17:59 -04:00
<< " m_steps: " << settings.m_steps
<< " m_step: " << settings.m_step
<< " m_sweepList: " << settings.m_sweepList
<< " m_setting: " << settings.m_setting
2021-06-09 11:17:59 -04:00
<< " m_visaDevice: " << settings.m_visaDevice
<< " m_powerOnSCPI: " << settings.m_powerOnSCPI
<< " m_powerOffSCPI: " << settings.m_powerOffSCPI
<< " m_powerOnCommand: " << settings.m_powerOnCommand
<< " m_powerOffCommand: " << settings.m_powerOffCommand
<< " m_powerDelay: " << settings.m_powerDelay
<< " m_enr: " << settings.m_enr
<< " m_streamIndex: " << settings.m_streamIndex
<< " m_useReverseAPI: " << settings.m_useReverseAPI
<< " m_reverseAPIAddress: " << settings.m_reverseAPIAddress
<< " m_reverseAPIPort: " << settings.m_reverseAPIPort
<< " m_reverseAPIDeviceIndex: " << settings.m_reverseAPIDeviceIndex
<< " m_reverseAPIChannelIndex: " << settings.m_reverseAPIChannelIndex
<< " force: " << force;
QList<QString> reverseAPIKeys;
if ((settings.m_inputFrequencyOffset != m_settings.m_inputFrequencyOffset) || force) {
reverseAPIKeys.append("inputFrequencyOffset");
}
if (m_settings.m_streamIndex != settings.m_streamIndex)
{
if (m_deviceAPI->getSampleMIMO()) // change of stream is possible for MIMO devices only
{
m_deviceAPI->removeChannelSinkAPI(this);
m_deviceAPI->removeChannelSink(this, m_settings.m_streamIndex);
m_deviceAPI->addChannelSink(this, settings.m_streamIndex);
m_deviceAPI->addChannelSinkAPI(this);
}
reverseAPIKeys.append("streamIndex");
}
NoiseFigureBaseband::MsgConfigureNoiseFigureBaseband *msg = NoiseFigureBaseband::MsgConfigureNoiseFigureBaseband::create(settings, force);
m_basebandSink->getInputMessageQueue()->push(msg);
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) ||
(m_settings.m_reverseAPIChannelIndex != settings.m_reverseAPIChannelIndex);
webapiReverseSendSettings(reverseAPIKeys, settings, fullUpdate || force);
}
m_settings = settings;
}
QByteArray NoiseFigure::serialize() const
{
return m_settings.serialize();
}
bool NoiseFigure::deserialize(const QByteArray& data)
{
if (m_settings.deserialize(data))
{
MsgConfigureNoiseFigure *msg = MsgConfigureNoiseFigure::create(m_settings, true);
m_inputMessageQueue.push(msg);
return true;
}
else
{
m_settings.resetToDefaults();
MsgConfigureNoiseFigure *msg = MsgConfigureNoiseFigure::create(m_settings, true);
m_inputMessageQueue.push(msg);
return false;
}
}
int NoiseFigure::webapiSettingsGet(
SWGSDRangel::SWGChannelSettings& response,
QString& errorMessage)
{
(void) errorMessage;
response.setNoiseFigureSettings(new SWGSDRangel::SWGNoiseFigureSettings());
response.getNoiseFigureSettings()->init();
webapiFormatChannelSettings(response, m_settings);
return 200;
}
int NoiseFigure::webapiWorkspaceGet(
SWGSDRangel::SWGWorkspaceInfo& response,
QString& errorMessage)
{
(void) errorMessage;
response.setIndex(m_settings.m_workspaceIndex);
return 200;
}
2021-06-09 11:17:59 -04:00
int NoiseFigure::webapiSettingsPutPatch(
bool force,
const QStringList& channelSettingsKeys,
SWGSDRangel::SWGChannelSettings& response,
QString& errorMessage)
{
(void) errorMessage;
NoiseFigureSettings settings = m_settings;
webapiUpdateChannelSettings(settings, channelSettingsKeys, response);
MsgConfigureNoiseFigure *msg = MsgConfigureNoiseFigure::create(settings, force);
m_inputMessageQueue.push(msg);
qDebug("NoiseFigure::webapiSettingsPutPatch: forward to GUI: %p", m_guiMessageQueue);
if (m_guiMessageQueue) // forward to GUI if any
{
MsgConfigureNoiseFigure *msgToGUI = MsgConfigureNoiseFigure::create(settings, force);
m_guiMessageQueue->push(msgToGUI);
}
webapiFormatChannelSettings(response, settings);
return 200;
}
void NoiseFigure::webapiUpdateChannelSettings(
NoiseFigureSettings& settings,
const QStringList& channelSettingsKeys,
SWGSDRangel::SWGChannelSettings& response)
{
if (channelSettingsKeys.contains("inputFrequencyOffset")) {
settings.m_inputFrequencyOffset = response.getNoiseFigureSettings()->getInputFrequencyOffset();
}
if (channelSettingsKeys.contains("fftSize")) {
settings.m_fftSize = response.getNoiseFigureSettings()->getFftSize();
}
if (channelSettingsKeys.contains("sweepSpec")) {
settings.m_sweepSpec = (NoiseFigureSettings::SweepSpec)response.getNoiseFigureSettings()->getSweepSpec();
2021-06-09 11:17:59 -04:00
}
if (channelSettingsKeys.contains("startValue")) {
settings.m_startValue = response.getNoiseFigureSettings()->getStartValue();
2021-06-09 11:17:59 -04:00
}
if (channelSettingsKeys.contains("stopValue")) {
settings.m_stopValue = response.getNoiseFigureSettings()->getStopValue();
2021-06-09 11:17:59 -04:00
}
if (channelSettingsKeys.contains("steps")) {
settings.m_steps = response.getNoiseFigureSettings()->getSteps();
}
if (channelSettingsKeys.contains("step")) {
settings.m_step = response.getNoiseFigureSettings()->getStep();
}
if (channelSettingsKeys.contains("list")) {
settings.m_sweepList = *response.getNoiseFigureSettings()->getList();
}
if (channelSettingsKeys.contains("setting")) {
settings.m_setting = *response.getNoiseFigureSettings()->getSetting();
2021-06-09 11:17:59 -04:00
}
if (channelSettingsKeys.contains("visaDevice")) {
settings.m_visaDevice = *response.getNoiseFigureSettings()->getVisaDevice();
}
if (channelSettingsKeys.contains("powerOnSCPI")) {
settings.m_powerOnSCPI = *response.getNoiseFigureSettings()->getPowerOnScpi();
}
if (channelSettingsKeys.contains("powerOffSCPI")) {
settings.m_powerOffSCPI = *response.getNoiseFigureSettings()->getPowerOffScpi();
}
if (channelSettingsKeys.contains("powerOnCommand")) {
settings.m_powerOnCommand = *response.getNoiseFigureSettings()->getPowerOnCommand();
}
if (channelSettingsKeys.contains("powerOffCommand")) {
settings.m_powerOffCommand = *response.getNoiseFigureSettings()->getPowerOffCommand();
}
if (channelSettingsKeys.contains("powerDelay")) {
settings.m_powerDelay = response.getNoiseFigureSettings()->getPowerDelay();
}
if (channelSettingsKeys.contains("rgbColor")) {
settings.m_rgbColor = response.getNoiseFigureSettings()->getRgbColor();
}
if (channelSettingsKeys.contains("title")) {
settings.m_title = *response.getNoiseFigureSettings()->getTitle();
}
if (channelSettingsKeys.contains("streamIndex")) {
settings.m_streamIndex = response.getNoiseFigureSettings()->getStreamIndex();
}
if (channelSettingsKeys.contains("useReverseAPI")) {
settings.m_useReverseAPI = response.getNoiseFigureSettings()->getUseReverseApi() != 0;
}
if (channelSettingsKeys.contains("reverseAPIAddress")) {
settings.m_reverseAPIAddress = *response.getNoiseFigureSettings()->getReverseApiAddress();
}
if (channelSettingsKeys.contains("reverseAPIPort")) {
settings.m_reverseAPIPort = response.getNoiseFigureSettings()->getReverseApiPort();
}
if (channelSettingsKeys.contains("reverseAPIDeviceIndex")) {
settings.m_reverseAPIDeviceIndex = response.getNoiseFigureSettings()->getReverseApiDeviceIndex();
}
if (channelSettingsKeys.contains("reverseAPIChannelIndex")) {
settings.m_reverseAPIChannelIndex = response.getNoiseFigureSettings()->getReverseApiChannelIndex();
}
if (settings.m_channelMarker && channelSettingsKeys.contains("channelMarker")) {
settings.m_channelMarker->updateFrom(channelSettingsKeys, response.getNoiseFigureSettings()->getChannelMarker());
}
if (settings.m_rollupState && channelSettingsKeys.contains("rollupState")) {
settings.m_rollupState->updateFrom(channelSettingsKeys, response.getNoiseFigureSettings()->getRollupState());
}
2021-06-09 11:17:59 -04:00
}
void NoiseFigure::webapiFormatChannelSettings(SWGSDRangel::SWGChannelSettings& response, const NoiseFigureSettings& settings)
{
response.getNoiseFigureSettings()->setInputFrequencyOffset(settings.m_inputFrequencyOffset);
response.getNoiseFigureSettings()->setRgbColor(settings.m_rgbColor);
if (response.getNoiseFigureSettings()->getTitle()) {
*response.getNoiseFigureSettings()->getTitle() = settings.m_title;
} else {
response.getNoiseFigureSettings()->setTitle(new QString(settings.m_title));
}
response.getNoiseFigureSettings()->setStreamIndex(settings.m_streamIndex);
response.getNoiseFigureSettings()->setUseReverseApi(settings.m_useReverseAPI ? 1 : 0);
if (response.getNoiseFigureSettings()->getReverseApiAddress()) {
*response.getNoiseFigureSettings()->getReverseApiAddress() = settings.m_reverseAPIAddress;
} else {
response.getNoiseFigureSettings()->setReverseApiAddress(new QString(settings.m_reverseAPIAddress));
}
response.getNoiseFigureSettings()->setReverseApiPort(settings.m_reverseAPIPort);
response.getNoiseFigureSettings()->setReverseApiDeviceIndex(settings.m_reverseAPIDeviceIndex);
response.getNoiseFigureSettings()->setReverseApiChannelIndex(settings.m_reverseAPIChannelIndex);
if (settings.m_channelMarker)
{
if (response.getNoiseFigureSettings()->getChannelMarker())
{
settings.m_channelMarker->formatTo(response.getNoiseFigureSettings()->getChannelMarker());
}
else
{
SWGSDRangel::SWGChannelMarker *swgChannelMarker = new SWGSDRangel::SWGChannelMarker();
settings.m_channelMarker->formatTo(swgChannelMarker);
response.getNoiseFigureSettings()->setChannelMarker(swgChannelMarker);
}
}
if (settings.m_rollupState)
{
if (response.getNoiseFigureSettings()->getRollupState())
{
settings.m_rollupState->formatTo(response.getNoiseFigureSettings()->getRollupState());
}
else
{
SWGSDRangel::SWGRollupState *swgRollupState = new SWGSDRangel::SWGRollupState();
settings.m_rollupState->formatTo(swgRollupState);
response.getNoiseFigureSettings()->setRollupState(swgRollupState);
}
}
2021-06-09 11:17:59 -04:00
}
void NoiseFigure::webapiReverseSendSettings(QList<QString>& channelSettingsKeys, const NoiseFigureSettings& settings, bool force)
{
SWGSDRangel::SWGChannelSettings *swgChannelSettings = new SWGSDRangel::SWGChannelSettings();
webapiFormatChannelSettings(channelSettingsKeys, swgChannelSettings, settings, force);
QString channelSettingsURL = QString("http://%1:%2/sdrangel/deviceset/%3/channel/%4/settings")
.arg(settings.m_reverseAPIAddress)
.arg(settings.m_reverseAPIPort)
.arg(settings.m_reverseAPIDeviceIndex)
.arg(settings.m_reverseAPIChannelIndex);
m_networkRequest.setUrl(QUrl(channelSettingsURL));
m_networkRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
QBuffer *buffer = new QBuffer();
buffer->open((QBuffer::ReadWrite));
buffer->write(swgChannelSettings->asJson().toUtf8());
buffer->seek(0);
// Always use PATCH to avoid passing reverse API settings
QNetworkReply *reply = m_networkManager->sendCustomRequest(m_networkRequest, "PATCH", buffer);
buffer->setParent(reply);
delete swgChannelSettings;
}
void NoiseFigure::webapiFormatChannelSettings(
QList<QString>& channelSettingsKeys,
SWGSDRangel::SWGChannelSettings *swgChannelSettings,
const NoiseFigureSettings& settings,
bool force
)
{
swgChannelSettings->setDirection(0); // Single sink (Rx)
swgChannelSettings->setOriginatorChannelIndex(getIndexInDeviceSet());
swgChannelSettings->setOriginatorDeviceSetIndex(getDeviceSetIndex());
swgChannelSettings->setChannelType(new QString("NoiseFigure"));
swgChannelSettings->setNoiseFigureSettings(new SWGSDRangel::SWGNoiseFigureSettings());
SWGSDRangel::SWGNoiseFigureSettings *swgNoiseFigureSettings = swgChannelSettings->getNoiseFigureSettings();
// transfer data that has been modified. When force is on transfer all data except reverse API data
if (channelSettingsKeys.contains("inputFrequencyOffset") || force) {
swgNoiseFigureSettings->setInputFrequencyOffset(settings.m_inputFrequencyOffset);
}
if (channelSettingsKeys.contains("fftSize") || force) {
swgNoiseFigureSettings->setFftSize(settings.m_fftSize);
}
if (channelSettingsKeys.contains("fftCount") || force) {
swgNoiseFigureSettings->setFftCount(settings.m_fftCount);
}
if (channelSettingsKeys.contains("sweepSpec") || force) {
swgNoiseFigureSettings->setSweepSpec((int)settings.m_sweepSpec);
}
if (channelSettingsKeys.contains("startValue") || force) {
swgNoiseFigureSettings->setStartValue(settings.m_startValue);
2021-06-09 11:17:59 -04:00
}
if (channelSettingsKeys.contains("stopValue") || force) {
swgNoiseFigureSettings->setStopValue(settings.m_stopValue);
2021-06-09 11:17:59 -04:00
}
if (channelSettingsKeys.contains("steps") || force) {
swgNoiseFigureSettings->setSteps(settings.m_steps);
}
if (channelSettingsKeys.contains("step") || force) {
swgNoiseFigureSettings->setStep(settings.m_step);
}
if (channelSettingsKeys.contains("list") || force) {
swgNoiseFigureSettings->setList(new QString(settings.m_sweepList));
}
if (channelSettingsKeys.contains("setting") || force) {
swgNoiseFigureSettings->setSetting(new QString(settings.m_setting));
2021-06-09 11:17:59 -04:00
}
if (channelSettingsKeys.contains("visaDevice") || force) {
swgNoiseFigureSettings->setVisaDevice(new QString(settings.m_visaDevice));
}
if (channelSettingsKeys.contains("powerOnSCPI") || force) {
swgNoiseFigureSettings->setPowerOnScpi(new QString(settings.m_powerOnSCPI));
}
if (channelSettingsKeys.contains("powerOffSCPI") || force) {
swgNoiseFigureSettings->setPowerOffScpi(new QString(settings.m_powerOffSCPI));
}
if (channelSettingsKeys.contains("powerOnCommand") || force) {
swgNoiseFigureSettings->setPowerOnCommand(new QString(settings.m_powerOnCommand));
}
if (channelSettingsKeys.contains("powerOffCommand") || force) {
swgNoiseFigureSettings->setPowerOffCommand(new QString(settings.m_powerOffCommand));
}
if (channelSettingsKeys.contains("powerDelay") || force) {
swgNoiseFigureSettings->setPowerDelay(settings.m_powerDelay);
}
if (channelSettingsKeys.contains("rgbColor") || force) {
swgNoiseFigureSettings->setRgbColor(settings.m_rgbColor);
}
if (channelSettingsKeys.contains("title") || force) {
swgNoiseFigureSettings->setTitle(new QString(settings.m_title));
}
if (channelSettingsKeys.contains("streamIndex") || force) {
swgNoiseFigureSettings->setStreamIndex(settings.m_streamIndex);
}
if (settings.m_channelMarker && (channelSettingsKeys.contains("channelMarker") || force))
{
SWGSDRangel::SWGChannelMarker *swgChannelMarker = new SWGSDRangel::SWGChannelMarker();
settings.m_channelMarker->formatTo(swgChannelMarker);
swgNoiseFigureSettings->setChannelMarker(swgChannelMarker);
}
if (settings.m_rollupState && (channelSettingsKeys.contains("rollupState") || force))
{
SWGSDRangel::SWGRollupState *swgRollupState = new SWGSDRangel::SWGRollupState();
settings.m_rollupState->formatTo(swgRollupState);
swgNoiseFigureSettings->setRollupState(swgRollupState);
}
2021-06-09 11:17:59 -04:00
}
void NoiseFigure::networkManagerFinished(QNetworkReply *reply)
{
QNetworkReply::NetworkError replyError = reply->error();
if (replyError)
{
qWarning() << "NoiseFigure::networkManagerFinished:"
<< " error(" << (int) replyError
<< "): " << replyError
<< ": " << reply->errorString();
}
else
{
QString answer = reply->readAll();
answer.chop(1); // remove last \n
qDebug("NoiseFigure::networkManagerFinished: reply:\n%s", answer.toStdString().c_str());
}
reply->deleteLater();
}
void NoiseFigure::handleIndexInDeviceSetChanged(int index)
{
if (index < 0) {
return;
}
QString fifoLabel = QString("%1 [%2:%3]")
.arg(m_channelId)
.arg(m_deviceAPI->getDeviceSetIndex())
.arg(index);
m_basebandSink->setFifoLabel(fifoLabel);
}