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

Reverse API: Daemon Source

This commit is contained in:
f4exb 2018-12-20 23:20:21 +01:00
parent 1803789400
commit 35702041a0
12 changed files with 356 additions and 8 deletions

View File

@ -21,7 +21,6 @@
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QBuffer>
#include <QJsonParseError>
#include <stdio.h>
#include <complex.h>

View File

@ -20,6 +20,9 @@
#include <boost/cstdint.hpp>
#include <QDebug>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QBuffer>
#include "SWGChannelSettings.h"
#include "SWGChannelReport.h"
@ -59,10 +62,15 @@ DaemonSource::DaemonSource(DeviceSinkAPI *deviceAPI) :
m_threadedChannelizer = new ThreadedBasebandSampleSource(m_channelizer, this);
m_deviceAPI->addThreadedSource(m_threadedChannelizer);
m_deviceAPI->addChannelAPI(this);
m_networkManager = new QNetworkAccessManager();
connect(m_networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(networkManagerFinished(QNetworkReply*)));
}
DaemonSource::~DaemonSource()
{
disconnect(m_networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(networkManagerFinished(QNetworkReply*)));
delete m_networkManager;
m_deviceAPI->removeChannelAPI(this);
m_deviceAPI->removeThreadedSource(m_threadedChannelizer);
delete m_threadedChannelizer;
@ -201,19 +209,36 @@ void DaemonSource::applySettings(const DaemonSourceSettings& settings, bool forc
<< " force: " << force;
bool change = false;
QList<QString> reverseAPIKeys;
if ((m_settings.m_dataAddress != settings.m_dataAddress) || force) {
if ((m_settings.m_dataAddress != settings.m_dataAddress) || force)
{
reverseAPIKeys.append("dataAddress");
change = true;
}
if ((m_settings.m_dataPort != settings.m_dataPort) || force) {
if ((m_settings.m_dataPort != settings.m_dataPort) || force)
{
reverseAPIKeys.append("dataPort");
change = true;
}
if (change && m_sourceThread) {
if (change && m_sourceThread)
{
reverseAPIKeys.append("sourceThread");
m_sourceThread->dataBind(settings.m_dataAddress, settings.m_dataPort);
}
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;
}
@ -392,6 +417,21 @@ int DaemonSource::webapiSettingsPutPatch(
if (channelSettingsKeys.contains("title")) {
settings.m_title = *response.getDaemonSourceSettings()->getTitle();
}
if (channelSettingsKeys.contains("useReverseAPI")) {
settings.m_useReverseAPI = response.getDaemonSourceSettings()->getUseReverseApi() != 0;
}
if (channelSettingsKeys.contains("reverseAPIAddress")) {
settings.m_reverseAPIAddress = *response.getDaemonSourceSettings()->getReverseApiAddress() != 0;
}
if (channelSettingsKeys.contains("reverseAPIPort")) {
settings.m_reverseAPIPort = response.getDaemonSourceSettings()->getReverseApiPort();
}
if (channelSettingsKeys.contains("reverseAPIDeviceIndex")) {
settings.m_reverseAPIDeviceIndex = response.getDaemonSourceSettings()->getReverseApiDeviceIndex();
}
if (channelSettingsKeys.contains("reverseAPIChannelIndex")) {
settings.m_reverseAPIChannelIndex = response.getDaemonSourceSettings()->getReverseApiChannelIndex();
}
MsgConfigureDaemonSource *msg = MsgConfigureDaemonSource::create(settings, force);
m_inputMessageQueue.push(msg);
@ -435,6 +475,18 @@ void DaemonSource::webapiFormatChannelSettings(SWGSDRangel::SWGChannelSettings&
} else {
response.getDaemonSourceSettings()->setTitle(new QString(settings.m_title));
}
response.getDaemonSourceSettings()->setUseReverseApi(settings.m_useReverseAPI ? 1 : 0);
if (response.getDaemonSourceSettings()->getReverseApiAddress()) {
*response.getDaemonSourceSettings()->getReverseApiAddress() = settings.m_reverseAPIAddress;
} else {
response.getDaemonSourceSettings()->setReverseApiAddress(new QString(settings.m_reverseAPIAddress));
}
response.getDaemonSourceSettings()->setReverseApiPort(settings.m_reverseAPIPort);
response.getDaemonSourceSettings()->setReverseApiDeviceIndex(settings.m_reverseAPIDeviceIndex);
response.getDaemonSourceSettings()->setReverseApiChannelIndex(settings.m_reverseAPIChannelIndex);
}
void DaemonSource::webapiFormatChannelReport(SWGSDRangel::SWGChannelReport& response)
@ -457,3 +509,62 @@ void DaemonSource::webapiFormatChannelReport(SWGSDRangel::SWGChannelReport& resp
response.getDaemonSourceReport()->setDeviceSampleRate(m_deviceAPI->getSampleSink()->getSampleRate());
}
void DaemonSource::webapiReverseSendSettings(QList<QString>& channelSettingsKeys, const DaemonSourceSettings& settings, bool force)
{
SWGSDRangel::SWGChannelSettings *swgChannelSettings = new SWGSDRangel::SWGChannelSettings();
swgChannelSettings->setTx(1);
swgChannelSettings->setChannelType(new QString("DaemonSource"));
swgChannelSettings->setDaemonSourceSettings(new SWGSDRangel::SWGDaemonSourceSettings());
SWGSDRangel::SWGDaemonSourceSettings *swgDaemonSourceSettings = swgChannelSettings->getDaemonSourceSettings();
// transfer data that has been modified. When force is on transfer all data except reverse API data
if (channelSettingsKeys.contains("dataAddress") || force) {
swgDaemonSourceSettings->setDataAddress(new QString(settings.m_dataAddress));
}
if (channelSettingsKeys.contains("dataPort") || force) {
swgDaemonSourceSettings->setDataPort(settings.m_dataPort);
}
if (channelSettingsKeys.contains("rgbColor") || force) {
swgDaemonSourceSettings->setRgbColor(settings.m_rgbColor);
}
if (channelSettingsKeys.contains("title") || force) {
swgDaemonSourceSettings->setTitle(new QString(settings.m_title));
}
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
m_networkManager->sendCustomRequest(m_networkRequest, "PATCH", buffer);
delete swgChannelSettings;
}
void DaemonSource::networkManagerFinished(QNetworkReply *reply)
{
QNetworkReply::NetworkError replyError = reply->error();
if (replyError)
{
qWarning() << "DaemonSource::networkManagerFinished:"
<< " error(" << (int) replyError
<< "): " << replyError
<< ": " << reply->errorString();
return;
}
QString answer = reply->readAll();
answer.chop(1); // remove last \n
qDebug("DaemonSource::networkManagerFinished: reply:\n%s", answer.toStdString().c_str());
}

View File

@ -18,6 +18,7 @@
#define PLUGINS_CHANNELTX_DAEMONSRC_DAEMONSRC_H_
#include <QObject>
#include <QNetworkRequest>
#include "cm256.h"
@ -35,6 +36,8 @@ class UpChannelizer;
class DeviceSinkAPI;
class DaemonSourceThread;
class SDRDaemonDataBlock;
class QNetworkAccessManager;
class QNetworkReply;
class DaemonSource : public BasebandSampleSource, public ChannelSourceAPI {
Q_OBJECT
@ -233,14 +236,19 @@ private:
uint32_t m_nbCorrectableErrors; //!< count of correctable errors in number of blocks
uint32_t m_nbUncorrectableErrors; //!< count of uncorrectable errors in number of blocks
QNetworkAccessManager *m_networkManager;
QNetworkRequest m_networkRequest;
void applySettings(const DaemonSourceSettings& settings, bool force = false);
void handleDataBlock(SDRDaemonDataBlock *dataBlock);
void printMeta(const QString& header, SDRDaemonMetaDataFEC *metaData);
uint32_t calculateDataReadQueueSize(int sampleRate);
void webapiFormatChannelSettings(SWGSDRangel::SWGChannelSettings& response, const DaemonSourceSettings& settings);
void webapiFormatChannelReport(SWGSDRangel::SWGChannelReport& response);
void webapiReverseSendSettings(QList<QString>& channelSettingsKeys, const DaemonSourceSettings& settings, bool force);
private slots:
void networkManagerFinished(QNetworkReply *reply);
void handleData();
};

View File

@ -273,11 +273,22 @@ void DaemonSourceGUI::onWidgetRolled(QWidget* widget, bool rollDown)
void DaemonSourceGUI::onMenuDialogCalled(const QPoint &p)
{
BasicChannelSettingsDialog dialog(&m_channelMarker, 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.setReverseAPIChannelIndex(m_settings.m_reverseAPIChannelIndex);
dialog.move(p);
dialog.exec();
m_settings.m_rgbColor = m_channelMarker.getColor().rgb();
m_settings.m_title = m_channelMarker.getTitle();
m_settings.m_useReverseAPI = dialog.useReverseAPI();
m_settings.m_reverseAPIAddress = dialog.getReverseAPIAddress();
m_settings.m_reverseAPIPort = dialog.getReverseAPIPort();
m_settings.m_reverseAPIDeviceIndex = dialog.getReverseAPIDeviceIndex();
m_settings.m_reverseAPIChannelIndex = dialog.getReverseAPIChannelIndex();
setWindowTitle(m_settings.m_title);
setTitleColor(m_settings.m_rgbColor);

View File

@ -32,6 +32,11 @@ void DaemonSourceSettings::resetToDefaults()
m_rgbColor = QColor(140, 4, 4).rgb();
m_title = "Daemon source";
m_channelMarker = nullptr;
m_useReverseAPI = false;
m_reverseAPIAddress = "127.0.0.1";
m_reverseAPIPort = 8888;
m_reverseAPIDeviceIndex = 0;
m_reverseAPIChannelIndex = 0;
}
QByteArray DaemonSourceSettings::serialize() const
@ -41,6 +46,11 @@ QByteArray DaemonSourceSettings::serialize() const
s.writeU32(2, m_dataPort);
s.writeU32(3, m_rgbColor);
s.writeString(4, m_title);
s.writeBool(5, m_useReverseAPI);
s.writeString(6, m_reverseAPIAddress);
s.writeU32(7, m_reverseAPIPort);
s.writeU32(8, m_reverseAPIDeviceIndex);
s.writeU32(9, m_reverseAPIChannelIndex);
return s.final();
}
@ -71,7 +81,20 @@ bool DaemonSourceSettings::deserialize(const QByteArray& data)
d.readU32(3, &m_rgbColor, QColor(0, 255, 255).rgb());
d.readString(4, &m_title, "Daemon source");
d.readBool(5, &m_useReverseAPI, false);
d.readString(6, &m_reverseAPIAddress, "127.0.0.1");
d.readU32(7, &tmp, 0);
if ((tmp > 1023) && (tmp < 65535)) {
m_reverseAPIPort = tmp;
} else {
m_reverseAPIPort = 8888;
}
d.readU32(8, &tmp, 0);
m_reverseAPIDeviceIndex = tmp > 99 ? 99 : tmp;
d.readU32(9, &tmp, 0);
m_reverseAPIChannelIndex = tmp > 99 ? 99 : tmp;
return true;
}
else

View File

@ -28,6 +28,11 @@ struct DaemonSourceSettings
uint16_t m_dataPort; //!< Listening data port
quint32 m_rgbColor;
QString m_title;
bool m_useReverseAPI;
QString m_reverseAPIAddress;
uint16_t m_reverseAPIPort;
uint16_t m_reverseAPIDeviceIndex;
uint16_t m_reverseAPIChannelIndex;
Serializable *m_channelMarker;

View File

@ -1995,6 +1995,22 @@ margin-bottom: 20px;
},
"title" : {
"type" : "string"
},
"useReverseAPI" : {
"type" : "integer",
"description" : "Synchronize with reverse API (1 for yes, 0 for no)"
},
"reverseAPIAddress" : {
"type" : "string"
},
"reverseAPIPort" : {
"type" : "integer"
},
"reverseAPIDeviceIndex" : {
"type" : "integer"
},
"reverseAPIChannelIndex" : {
"type" : "integer"
}
},
"description" : "Daemon channel source settings"
@ -23749,7 +23765,7 @@ except ApiException as e:
</div>
<div id="generator">
<div class="content">
Generated 2018-12-20T08:23:16.509+01:00
Generated 2018-12-20T23:13:50.969+01:00
</div>
</div>
</div>

View File

@ -11,6 +11,17 @@ DaemonSourceSettings:
type: integer
title:
type: string
useReverseAPI:
description: Synchronize with reverse API (1 for yes, 0 for no)
type: integer
reverseAPIAddress:
type: string
reverseAPIPort:
type: integer
reverseAPIDeviceIndex:
type: integer
reverseAPIChannelIndex:
type: integer
DaemonSourceReport:
description: "Daemon channel source report"

View File

@ -11,6 +11,17 @@ DaemonSourceSettings:
type: integer
title:
type: string
useReverseAPI:
description: Synchronize with reverse API (1 for yes, 0 for no)
type: integer
reverseAPIAddress:
type: string
reverseAPIPort:
type: integer
reverseAPIDeviceIndex:
type: integer
reverseAPIChannelIndex:
type: integer
DaemonSourceReport:
description: "Daemon channel source report"

View File

@ -1995,6 +1995,22 @@ margin-bottom: 20px;
},
"title" : {
"type" : "string"
},
"useReverseAPI" : {
"type" : "integer",
"description" : "Synchronize with reverse API (1 for yes, 0 for no)"
},
"reverseAPIAddress" : {
"type" : "string"
},
"reverseAPIPort" : {
"type" : "integer"
},
"reverseAPIDeviceIndex" : {
"type" : "integer"
},
"reverseAPIChannelIndex" : {
"type" : "integer"
}
},
"description" : "Daemon channel source settings"
@ -23749,7 +23765,7 @@ except ApiException as e:
</div>
<div id="generator">
<div class="content">
Generated 2018-12-20T08:23:16.509+01:00
Generated 2018-12-20T23:13:50.969+01:00
</div>
</div>
</div>

View File

@ -36,6 +36,16 @@ SWGDaemonSourceSettings::SWGDaemonSourceSettings() {
m_rgb_color_isSet = false;
title = nullptr;
m_title_isSet = false;
use_reverse_api = 0;
m_use_reverse_api_isSet = false;
reverse_api_address = nullptr;
m_reverse_api_address_isSet = false;
reverse_api_port = 0;
m_reverse_api_port_isSet = false;
reverse_api_device_index = 0;
m_reverse_api_device_index_isSet = false;
reverse_api_channel_index = 0;
m_reverse_api_channel_index_isSet = false;
}
SWGDaemonSourceSettings::~SWGDaemonSourceSettings() {
@ -52,6 +62,16 @@ SWGDaemonSourceSettings::init() {
m_rgb_color_isSet = false;
title = new QString("");
m_title_isSet = false;
use_reverse_api = 0;
m_use_reverse_api_isSet = false;
reverse_api_address = new QString("");
m_reverse_api_address_isSet = false;
reverse_api_port = 0;
m_reverse_api_port_isSet = false;
reverse_api_device_index = 0;
m_reverse_api_device_index_isSet = false;
reverse_api_channel_index = 0;
m_reverse_api_channel_index_isSet = false;
}
void
@ -64,6 +84,13 @@ SWGDaemonSourceSettings::cleanup() {
if(title != nullptr) {
delete title;
}
if(reverse_api_address != nullptr) {
delete reverse_api_address;
}
}
SWGDaemonSourceSettings*
@ -85,6 +112,16 @@ SWGDaemonSourceSettings::fromJsonObject(QJsonObject &pJson) {
::SWGSDRangel::setValue(&title, pJson["title"], "QString", "QString");
::SWGSDRangel::setValue(&use_reverse_api, pJson["useReverseAPI"], "qint32", "");
::SWGSDRangel::setValue(&reverse_api_address, pJson["reverseAPIAddress"], "QString", "QString");
::SWGSDRangel::setValue(&reverse_api_port, pJson["reverseAPIPort"], "qint32", "");
::SWGSDRangel::setValue(&reverse_api_device_index, pJson["reverseAPIDeviceIndex"], "qint32", "");
::SWGSDRangel::setValue(&reverse_api_channel_index, pJson["reverseAPIChannelIndex"], "qint32", "");
}
QString
@ -113,6 +150,21 @@ SWGDaemonSourceSettings::asJsonObject() {
if(title != nullptr && *title != QString("")){
toJsonValue(QString("title"), title, obj, QString("QString"));
}
if(m_use_reverse_api_isSet){
obj->insert("useReverseAPI", QJsonValue(use_reverse_api));
}
if(reverse_api_address != nullptr && *reverse_api_address != QString("")){
toJsonValue(QString("reverseAPIAddress"), reverse_api_address, obj, QString("QString"));
}
if(m_reverse_api_port_isSet){
obj->insert("reverseAPIPort", QJsonValue(reverse_api_port));
}
if(m_reverse_api_device_index_isSet){
obj->insert("reverseAPIDeviceIndex", QJsonValue(reverse_api_device_index));
}
if(m_reverse_api_channel_index_isSet){
obj->insert("reverseAPIChannelIndex", QJsonValue(reverse_api_channel_index));
}
return obj;
}
@ -157,6 +209,56 @@ SWGDaemonSourceSettings::setTitle(QString* title) {
this->m_title_isSet = true;
}
qint32
SWGDaemonSourceSettings::getUseReverseApi() {
return use_reverse_api;
}
void
SWGDaemonSourceSettings::setUseReverseApi(qint32 use_reverse_api) {
this->use_reverse_api = use_reverse_api;
this->m_use_reverse_api_isSet = true;
}
QString*
SWGDaemonSourceSettings::getReverseApiAddress() {
return reverse_api_address;
}
void
SWGDaemonSourceSettings::setReverseApiAddress(QString* reverse_api_address) {
this->reverse_api_address = reverse_api_address;
this->m_reverse_api_address_isSet = true;
}
qint32
SWGDaemonSourceSettings::getReverseApiPort() {
return reverse_api_port;
}
void
SWGDaemonSourceSettings::setReverseApiPort(qint32 reverse_api_port) {
this->reverse_api_port = reverse_api_port;
this->m_reverse_api_port_isSet = true;
}
qint32
SWGDaemonSourceSettings::getReverseApiDeviceIndex() {
return reverse_api_device_index;
}
void
SWGDaemonSourceSettings::setReverseApiDeviceIndex(qint32 reverse_api_device_index) {
this->reverse_api_device_index = reverse_api_device_index;
this->m_reverse_api_device_index_isSet = true;
}
qint32
SWGDaemonSourceSettings::getReverseApiChannelIndex() {
return reverse_api_channel_index;
}
void
SWGDaemonSourceSettings::setReverseApiChannelIndex(qint32 reverse_api_channel_index) {
this->reverse_api_channel_index = reverse_api_channel_index;
this->m_reverse_api_channel_index_isSet = true;
}
bool
SWGDaemonSourceSettings::isSet(){
@ -166,6 +268,11 @@ SWGDaemonSourceSettings::isSet(){
if(m_data_port_isSet){ isObjectUpdated = true; break;}
if(m_rgb_color_isSet){ isObjectUpdated = true; break;}
if(title != nullptr && *title != QString("")){ isObjectUpdated = true; break;}
if(m_use_reverse_api_isSet){ isObjectUpdated = true; break;}
if(reverse_api_address != nullptr && *reverse_api_address != QString("")){ isObjectUpdated = true; break;}
if(m_reverse_api_port_isSet){ isObjectUpdated = true; break;}
if(m_reverse_api_device_index_isSet){ isObjectUpdated = true; break;}
if(m_reverse_api_channel_index_isSet){ isObjectUpdated = true; break;}
}while(false);
return isObjectUpdated;
}

View File

@ -54,6 +54,21 @@ public:
QString* getTitle();
void setTitle(QString* title);
qint32 getUseReverseApi();
void setUseReverseApi(qint32 use_reverse_api);
QString* getReverseApiAddress();
void setReverseApiAddress(QString* reverse_api_address);
qint32 getReverseApiPort();
void setReverseApiPort(qint32 reverse_api_port);
qint32 getReverseApiDeviceIndex();
void setReverseApiDeviceIndex(qint32 reverse_api_device_index);
qint32 getReverseApiChannelIndex();
void setReverseApiChannelIndex(qint32 reverse_api_channel_index);
virtual bool isSet() override;
@ -70,6 +85,21 @@ private:
QString* title;
bool m_title_isSet;
qint32 use_reverse_api;
bool m_use_reverse_api_isSet;
QString* reverse_api_address;
bool m_reverse_api_address_isSet;
qint32 reverse_api_port;
bool m_reverse_api_port_isSet;
qint32 reverse_api_device_index;
bool m_reverse_api_device_index_isSet;
qint32 reverse_api_channel_index;
bool m_reverse_api_channel_index_isSet;
};
}