1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-12-25 12:12:02 -05:00
sdrangel/plugins/channeltx/remotesource/remotesource.cpp

422 lines
17 KiB
C++
Raw Normal View History

2018-08-31 17:29:53 -04:00
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2018-2019 Edouard Griffiths, F4EXB //
2018-08-31 17:29:53 -04:00
// //
// 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 //
2019-04-11 00:39:30 -04:00
// (at your option) any later version. //
2018-08-31 17:29:53 -04:00
// //
// 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 "remotesource.h"
2018-08-31 18:18:35 -04:00
#include <QDebug>
2018-12-20 17:20:21 -05:00
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QBuffer>
#include <QThread>
2018-08-31 18:18:35 -04:00
2018-08-31 22:37:23 -04:00
#include "SWGChannelSettings.h"
#include "SWGChannelReport.h"
#include "SWGRemoteSourceReport.h"
2018-08-31 22:37:23 -04:00
2018-08-31 22:43:15 -04:00
#include "dsp/devicesamplesink.h"
2019-05-08 16:11:53 -04:00
#include "device/deviceapi.h"
#include "util/timeutil.h"
2018-08-31 17:29:53 -04:00
#include "remotesourcebaseband.h"
2018-08-31 17:29:53 -04:00
MESSAGE_CLASS_DEFINITION(RemoteSource::MsgConfigureRemoteSource, Message)
MESSAGE_CLASS_DEFINITION(RemoteSource::MsgQueryStreamData, Message)
MESSAGE_CLASS_DEFINITION(RemoteSource::MsgReportStreamData, Message)
2018-08-31 18:18:35 -04:00
const QString RemoteSource::m_channelIdURI = "sdrangel.channeltx.remotesource";
const QString RemoteSource::m_channelId ="RemoteSource";
2018-08-31 17:29:53 -04:00
2019-05-08 16:11:53 -04:00
RemoteSource::RemoteSource(DeviceAPI *deviceAPI) :
2019-05-09 11:27:12 -04:00
ChannelAPI(m_channelIdURI, ChannelAPI::StreamSingleSource),
m_deviceAPI(deviceAPI)
2018-08-31 17:29:53 -04:00
{
setObjectName(m_channelId);
m_thread = new QThread(this);
m_basebandSource = new RemoteSourceBaseband();
m_basebandSource->moveToThread(m_thread);
applySettings(m_settings, true);
m_deviceAPI->addChannelSource(this);
2019-05-08 16:11:53 -04:00
m_deviceAPI->addChannelSourceAPI(this);
2018-12-20 17:20:21 -05:00
m_networkManager = new QNetworkAccessManager();
connect(m_networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(networkManagerFinished(QNetworkReply*)));
2018-08-31 17:29:53 -04:00
}
RemoteSource::~RemoteSource()
2018-08-31 17:29:53 -04:00
{
2018-12-20 17:20:21 -05:00
disconnect(m_networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(networkManagerFinished(QNetworkReply*)));
delete m_networkManager;
2019-05-08 16:11:53 -04:00
m_deviceAPI->removeChannelSourceAPI(this);
m_deviceAPI->removeChannelSource(this);
delete m_basebandSource;
delete m_thread;
2018-08-31 17:29:53 -04:00
}
void RemoteSource::start()
2018-08-31 17:29:53 -04:00
{
qDebug("RemoteSource::start");
m_basebandSource->reset();
m_thread->start();
RemoteSourceBaseband::MsgConfigureRemoteSourceWork *msg = RemoteSourceBaseband::MsgConfigureRemoteSourceWork::create(true);
m_basebandSource->getInputMessageQueue()->push(msg);
2018-08-31 17:29:53 -04:00
}
void RemoteSource::stop()
2018-08-31 17:29:53 -04:00
{
qDebug("RemoteSource::stop");
m_thread->exit();
m_thread->wait();
2018-08-31 22:37:23 -04:00
}
void RemoteSource::pull(SampleVector::iterator& begin, unsigned int nbSamples)
2018-08-31 22:37:23 -04:00
{
m_basebandSource->pull(begin, nbSamples);
2018-08-31 17:29:53 -04:00
}
bool RemoteSource::handleMessage(const Message& cmd)
2018-08-31 17:29:53 -04:00
{
if (MsgConfigureRemoteSource::match(cmd))
2018-08-31 22:37:23 -04:00
{
MsgConfigureRemoteSource& cfg = (MsgConfigureRemoteSource&) cmd;
qDebug() << "MsgConfigureRemoteSource::handleMessage: MsgConfigureRemoteSource";
2018-08-31 22:37:23 -04:00
applySettings(cfg.getSettings(), cfg.getForce());
return true;
}
else if (MsgQueryStreamData::match(cmd))
{
if (m_guiMessageQueue)
{
uint64_t nowus = TimeUtil::nowus();
RemoteDataReadQueue& dataReadQueue = m_basebandSource->getDataQueue();
const RemoteMetaDataFEC& currentMeta = m_basebandSource->getRemoteMetaDataFEC();
MsgReportStreamData *msg = MsgReportStreamData::create(
nowus / 1000000U,
nowus % 1000000U,
dataReadQueue.size(),
dataReadQueue.length(),
dataReadQueue.readSampleCount(),
m_basebandSource->getNbCorrectableErrors(),
m_basebandSource->getNbUncorrectableErrors(),
currentMeta.m_nbOriginalBlocks,
currentMeta.m_nbFECBlocks,
currentMeta.m_centerFrequency,
currentMeta.m_sampleRate);
m_guiMessageQueue->push(msg);
}
return true;
}
2018-08-31 17:29:53 -04:00
return false;
}
QByteArray RemoteSource::serialize() const
2018-08-31 17:29:53 -04:00
{
return m_settings.serialize();
}
bool RemoteSource::deserialize(const QByteArray& data)
2018-08-31 17:29:53 -04:00
{
(void) data;
2018-08-31 17:29:53 -04:00
if (m_settings.deserialize(data))
{
MsgConfigureRemoteSource *msg = MsgConfigureRemoteSource::create(m_settings, true);
2018-08-31 22:37:23 -04:00
m_inputMessageQueue.push(msg);
2018-08-31 17:29:53 -04:00
return true;
}
else
{
m_settings.resetToDefaults();
MsgConfigureRemoteSource *msg = MsgConfigureRemoteSource::create(m_settings, true);
2018-08-31 22:37:23 -04:00
m_inputMessageQueue.push(msg);
2018-08-31 17:29:53 -04:00
return false;
}
}
2018-08-31 22:37:23 -04:00
void RemoteSource::applySettings(const RemoteSourceSettings& settings, bool force)
2018-08-31 22:37:23 -04:00
{
qDebug() << "RemoteSource::applySettings:"
2018-08-31 22:37:23 -04:00
<< " m_dataAddress: " << settings.m_dataAddress
<< " m_dataPort: " << settings.m_dataPort
<< " m_rgbColor: " << settings.m_rgbColor
<< " m_title: " << settings.m_title
<< " m_useReverseAPI: " << settings.m_useReverseAPI
<< " m_reverseAPIAddress: " << settings.m_reverseAPIAddress
<< " m_reverseAPIChannelIndex: " << settings.m_reverseAPIChannelIndex
<< " m_reverseAPIDeviceIndex: " << settings.m_reverseAPIDeviceIndex
<< " m_reverseAPIPort: " << settings.m_reverseAPIPort
2018-08-31 22:37:23 -04:00
<< " force: " << force;
bool change = false;
2018-12-20 17:20:21 -05:00
QList<QString> reverseAPIKeys;
2018-08-31 22:37:23 -04:00
if ((m_settings.m_dataAddress != settings.m_dataAddress) || force) {
2018-12-20 17:20:21 -05:00
reverseAPIKeys.append("dataAddress");
2018-08-31 22:37:23 -04:00
}
if ((m_settings.m_dataPort != settings.m_dataPort) || force) {
2018-12-20 17:20:21 -05:00
reverseAPIKeys.append("dataPort");
2018-08-31 22:37:23 -04:00
}
if (m_settings.m_streamIndex != settings.m_streamIndex)
{
if (m_deviceAPI->getSampleMIMO()) // change of stream is possible for MIMO devices only
{
m_deviceAPI->removeChannelSourceAPI(this, m_settings.m_streamIndex);
m_deviceAPI->removeChannelSource(this, m_settings.m_streamIndex);
m_deviceAPI->addChannelSource(this, settings.m_streamIndex);
m_deviceAPI->addChannelSourceAPI(this, settings.m_streamIndex);
}
reverseAPIKeys.append("streamIndex");
}
RemoteSourceBaseband::MsgConfigureRemoteSourceBaseband *msg = RemoteSourceBaseband::MsgConfigureRemoteSourceBaseband::create(settings, force);
m_basebandSource->getInputMessageQueue()->push(msg);
2018-08-31 22:37:23 -04:00
2018-12-20 17:20:21 -05:00
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);
}
2018-08-31 22:37:23 -04:00
m_settings = settings;
}
int RemoteSource::webapiSettingsGet(
2018-08-31 22:37:23 -04:00
SWGSDRangel::SWGChannelSettings& response,
QString& errorMessage)
2018-08-31 22:37:23 -04:00
{
(void) errorMessage;
response.setRemoteSourceSettings(new SWGSDRangel::SWGRemoteSourceSettings());
response.getRemoteSourceSettings()->init();
2018-08-31 22:37:23 -04:00
webapiFormatChannelSettings(response, m_settings);
return 200;
}
int RemoteSource::webapiSettingsPutPatch(
2018-08-31 22:37:23 -04:00
bool force,
const QStringList& channelSettingsKeys,
SWGSDRangel::SWGChannelSettings& response,
QString& errorMessage)
2018-08-31 22:37:23 -04:00
{
(void) errorMessage;
RemoteSourceSettings settings = m_settings;
webapiUpdateChannelSettings(settings, channelSettingsKeys, response);
2018-08-31 22:37:23 -04:00
MsgConfigureRemoteSource *msg = MsgConfigureRemoteSource::create(settings, force);
m_inputMessageQueue.push(msg);
qDebug("RemoteSource::webapiSettingsPutPatch: forward to GUI: %p", m_guiMessageQueue);
if (m_guiMessageQueue) // forward to GUI if any
{
MsgConfigureRemoteSource *msgToGUI = MsgConfigureRemoteSource::create(settings, force);
m_guiMessageQueue->push(msgToGUI);
}
webapiFormatChannelSettings(response, settings);
return 200;
}
void RemoteSource::webapiUpdateChannelSettings(
RemoteSourceSettings& settings,
const QStringList& channelSettingsKeys,
SWGSDRangel::SWGChannelSettings& response)
{
2018-08-31 22:37:23 -04:00
if (channelSettingsKeys.contains("dataAddress")) {
settings.m_dataAddress = *response.getRemoteSourceSettings()->getDataAddress();
2018-08-31 22:37:23 -04:00
}
if (channelSettingsKeys.contains("dataPort"))
{
int dataPort = response.getRemoteSourceSettings()->getDataPort();
2018-08-31 22:37:23 -04:00
if ((dataPort < 1024) || (dataPort > 65535)) {
settings.m_dataPort = 9090;
} else {
settings.m_dataPort = dataPort;
}
}
2018-09-01 03:53:16 -04:00
if (channelSettingsKeys.contains("rgbColor")) {
settings.m_rgbColor = response.getRemoteSourceSettings()->getRgbColor();
2018-09-01 03:53:16 -04:00
}
if (channelSettingsKeys.contains("title")) {
settings.m_title = *response.getRemoteSourceSettings()->getTitle();
2018-09-01 03:53:16 -04:00
}
if (channelSettingsKeys.contains("streamIndex")) {
settings.m_streamIndex = response.getRemoteSourceSettings()->getStreamIndex();
}
2018-12-20 17:20:21 -05:00
if (channelSettingsKeys.contains("useReverseAPI")) {
settings.m_useReverseAPI = response.getRemoteSourceSettings()->getUseReverseApi() != 0;
2018-12-20 17:20:21 -05:00
}
if (channelSettingsKeys.contains("reverseAPIAddress")) {
settings.m_reverseAPIAddress = *response.getRemoteSourceSettings()->getReverseApiAddress();
2018-12-20 17:20:21 -05:00
}
if (channelSettingsKeys.contains("reverseAPIPort")) {
settings.m_reverseAPIPort = response.getRemoteSourceSettings()->getReverseApiPort();
2018-12-20 17:20:21 -05:00
}
if (channelSettingsKeys.contains("reverseAPIDeviceIndex")) {
settings.m_reverseAPIDeviceIndex = response.getRemoteSourceSettings()->getReverseApiDeviceIndex();
2018-12-20 17:20:21 -05:00
}
if (channelSettingsKeys.contains("reverseAPIChannelIndex")) {
settings.m_reverseAPIChannelIndex = response.getRemoteSourceSettings()->getReverseApiChannelIndex();
2018-12-20 17:20:21 -05:00
}
2018-08-31 22:37:23 -04:00
}
int RemoteSource::webapiReportGet(
2018-08-31 22:37:23 -04:00
SWGSDRangel::SWGChannelReport& response,
QString& errorMessage)
2018-08-31 22:37:23 -04:00
{
(void) errorMessage;
response.setRemoteSourceReport(new SWGSDRangel::SWGRemoteSourceReport());
response.getRemoteSourceReport()->init();
2018-08-31 22:37:23 -04:00
webapiFormatChannelReport(response);
return 200;
}
void RemoteSource::webapiFormatChannelSettings(SWGSDRangel::SWGChannelSettings& response, const RemoteSourceSettings& settings)
2018-08-31 22:37:23 -04:00
{
if (response.getRemoteSourceSettings()->getDataAddress()) {
*response.getRemoteSourceSettings()->getDataAddress() = settings.m_dataAddress;
2018-08-31 22:37:23 -04:00
} else {
response.getRemoteSourceSettings()->setDataAddress(new QString(settings.m_dataAddress));
2018-08-31 22:37:23 -04:00
}
response.getRemoteSourceSettings()->setDataPort(settings.m_dataPort);
response.getRemoteSourceSettings()->setRgbColor(settings.m_rgbColor);
2018-09-01 03:53:16 -04:00
if (response.getRemoteSourceSettings()->getTitle()) {
*response.getRemoteSourceSettings()->getTitle() = settings.m_title;
2018-09-01 03:53:16 -04:00
} else {
response.getRemoteSourceSettings()->setTitle(new QString(settings.m_title));
2018-09-01 03:53:16 -04:00
}
2018-12-20 17:20:21 -05:00
response.getRemoteSourceSettings()->setUseReverseApi(settings.m_useReverseAPI ? 1 : 0);
2018-12-20 17:20:21 -05:00
if (response.getRemoteSourceSettings()->getReverseApiAddress()) {
*response.getRemoteSourceSettings()->getReverseApiAddress() = settings.m_reverseAPIAddress;
2018-12-20 17:20:21 -05:00
} else {
response.getRemoteSourceSettings()->setReverseApiAddress(new QString(settings.m_reverseAPIAddress));
2018-12-20 17:20:21 -05:00
}
response.getRemoteSourceSettings()->setReverseApiPort(settings.m_reverseAPIPort);
response.getRemoteSourceSettings()->setReverseApiDeviceIndex(settings.m_reverseAPIDeviceIndex);
response.getRemoteSourceSettings()->setReverseApiChannelIndex(settings.m_reverseAPIChannelIndex);
2018-08-31 22:37:23 -04:00
}
void RemoteSource::webapiFormatChannelReport(SWGSDRangel::SWGChannelReport& response)
2018-08-31 22:37:23 -04:00
{
uint64_t nowus = TimeUtil::nowus();
RemoteDataReadQueue& dataReadQueue = m_basebandSource->getDataQueue();
const RemoteMetaDataFEC& currentMeta = m_basebandSource->getRemoteMetaDataFEC();
response.getRemoteSourceReport()->setTvSec(nowus / 1000000U);
response.getRemoteSourceReport()->setTvUSec(nowus % 1000000U);
response.getRemoteSourceReport()->setQueueSize(dataReadQueue.size());
response.getRemoteSourceReport()->setQueueLength(dataReadQueue.length());
response.getRemoteSourceReport()->setSamplesCount(dataReadQueue.readSampleCount());
response.getRemoteSourceReport()->setCorrectableErrorsCount(m_basebandSource->getNbCorrectableErrors());
response.getRemoteSourceReport()->setUncorrectableErrorsCount(m_basebandSource->getNbUncorrectableErrors());
response.getRemoteSourceReport()->setNbOriginalBlocks(currentMeta.m_nbOriginalBlocks);
response.getRemoteSourceReport()->setNbFecBlocks(currentMeta.m_nbFECBlocks);
response.getRemoteSourceReport()->setCenterFreq(currentMeta.m_centerFrequency);
response.getRemoteSourceReport()->setSampleRate(currentMeta.m_sampleRate);
response.getRemoteSourceReport()->setDeviceCenterFreq(m_deviceAPI->getSampleSink()->getCenterFrequency()/1000);
response.getRemoteSourceReport()->setDeviceSampleRate(m_deviceAPI->getSampleSink()->getSampleRate());
2018-08-31 22:37:23 -04:00
}
void RemoteSource::webapiReverseSendSettings(QList<QString>& channelSettingsKeys, const RemoteSourceSettings& settings, bool force)
2018-12-20 17:20:21 -05:00
{
SWGSDRangel::SWGChannelSettings *swgChannelSettings = new SWGSDRangel::SWGChannelSettings();
swgChannelSettings->setDirection(1); // single source (Tx)
swgChannelSettings->setOriginatorChannelIndex(getIndexInDeviceSet());
swgChannelSettings->setOriginatorDeviceSetIndex(getDeviceSetIndex());
swgChannelSettings->setChannelType(new QString("RemoteSource"));
swgChannelSettings->setRemoteSourceSettings(new SWGSDRangel::SWGRemoteSourceSettings());
SWGSDRangel::SWGRemoteSourceSettings *swgRemoteSourceSettings = swgChannelSettings->getRemoteSourceSettings();
2018-12-20 17:20:21 -05:00
// transfer data that has been modified. When force is on transfer all data except reverse API data
if (channelSettingsKeys.contains("dataAddress") || force) {
swgRemoteSourceSettings->setDataAddress(new QString(settings.m_dataAddress));
2018-12-20 17:20:21 -05:00
}
if (channelSettingsKeys.contains("dataPort") || force) {
swgRemoteSourceSettings->setDataPort(settings.m_dataPort);
2018-12-20 17:20:21 -05:00
}
if (channelSettingsKeys.contains("rgbColor") || force) {
swgRemoteSourceSettings->setRgbColor(settings.m_rgbColor);
2018-12-20 17:20:21 -05:00
}
if (channelSettingsKeys.contains("title") || force) {
swgRemoteSourceSettings->setTitle(new QString(settings.m_title));
2018-12-20 17:20:21 -05:00
}
if (channelSettingsKeys.contains("streamIndex") || force) {
swgRemoteSourceSettings->setStreamIndex(settings.m_streamIndex);
}
2018-12-20 17:20:21 -05:00
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();
2018-12-20 17:20:21 -05:00
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);
2018-12-20 17:20:21 -05:00
delete swgChannelSettings;
}
void RemoteSource::networkManagerFinished(QNetworkReply *reply)
2018-12-20 17:20:21 -05:00
{
QNetworkReply::NetworkError replyError = reply->error();
if (replyError)
{
qWarning() << "RemoteSource::networkManagerFinished:"
2018-12-20 17:20:21 -05:00
<< " error(" << (int) replyError
<< "): " << replyError
<< ": " << reply->errorString();
}
else
{
QString answer = reply->readAll();
answer.chop(1); // remove last \n
qDebug("RemoteSource::networkManagerFinished: reply:\n%s", answer.toStdString().c_str());
2018-12-20 17:20:21 -05:00
}
reply->deleteLater();
2018-12-20 17:20:21 -05:00
}
uint32_t RemoteSource::getNumberOfDeviceStreams() const
{
return m_deviceAPI->getNbSinkStreams();
}