From 7d076e5c9ce616498f5d6512fcf73f0a42f55a03 Mon Sep 17 00:00:00 2001 From: f4exb Date: Wed, 6 May 2020 09:50:43 +0200 Subject: [PATCH] Websocket spectrum: server implementation --- sdrbase/dsp/spectrumvis.cpp | 3 + sdrsrv/device/deviceset.cpp | 38 ++++++++++- sdrsrv/device/deviceset.h | 21 +++++- sdrsrv/maincore.cpp | 10 ++- sdrsrv/webapi/webapiadaptersrv.cpp | 103 +++++++++++++++++++++++++++++ sdrsrv/webapi/webapiadaptersrv.h | 27 ++++++++ 6 files changed, 197 insertions(+), 5 deletions(-) diff --git a/sdrbase/dsp/spectrumvis.cpp b/sdrbase/dsp/spectrumvis.cpp index 6f1513976..15a790eec 100644 --- a/sdrbase/dsp/spectrumvis.cpp +++ b/sdrbase/dsp/spectrumvis.cpp @@ -20,6 +20,7 @@ #include "SWGGLSpectrum.h" #include "SWGSpectrumServer.h" +#include "SWGSuccessResponse.h" #include "glspectruminterface.h" #include "dspcommands.h" @@ -816,6 +817,7 @@ int SpectrumVis::webapiSpectrumServerPost(SWGSDRangel::SWGSuccessResponse& respo getMessageQueueToGUI()->push(msgToGui); } + response.setMessage(new QString("Websocket spectrum server started")); return 200; } @@ -831,6 +833,7 @@ int SpectrumVis::webapiSpectrumServerDelete(SWGSDRangel::SWGSuccessResponse& res getMessageQueueToGUI()->push(msgToGui); } + response.setMessage(new QString("Websocket spectrum server stopped")); return 200; } diff --git a/sdrsrv/device/deviceset.cpp b/sdrsrv/device/deviceset.cpp index c28e6820c..bc6f86ce4 100644 --- a/sdrsrv/device/deviceset.cpp +++ b/sdrsrv/device/deviceset.cpp @@ -19,6 +19,7 @@ #include "dsp/dspdevicesourceengine.h" #include "dsp/dspdevicesinkengine.h" +#include "dsp/spectrumvis.h" #include "plugin/pluginapi.h" #include "plugin/plugininterface.h" #include "settings/preset.h" @@ -34,17 +35,24 @@ DeviceSet::ChannelInstanceRegistration::ChannelInstanceRegistration(const QStrin m_channelAPI(channelAPI) {} -DeviceSet::DeviceSet(int tabIndex) +DeviceSet::DeviceSet(int tabIndex, int deviceType) { m_deviceAPI = nullptr; m_deviceSourceEngine = nullptr; m_deviceSinkEngine = nullptr; m_deviceMIMOEngine = nullptr; m_deviceTabIndex = tabIndex; + + if ((deviceType == 0) || (deviceType == 2)) { // Single Rx or MIMO + m_spectrumVis = new SpectrumVis(SDR_RX_SCALEF); + } else if (deviceType == 1) { // Single Tx + m_spectrumVis = new SpectrumVis(SDR_TX_SCALEF); + } } DeviceSet::~DeviceSet() { + delete m_spectrumVis; } void DeviceSet::registerRxChannelInstance(const QString& channelName, ChannelAPI* channelAPI) @@ -466,3 +474,31 @@ bool DeviceSet::ChannelInstanceRegistration::operator<(const ChannelInstanceRegi } } +int DeviceSet::webapiSpectrumSettingsGet(SWGSDRangel::SWGGLSpectrum& response, QString& errorMessage) const +{ + return m_spectrumVis->webapiSpectrumSettingsGet(response, errorMessage); +} + +int DeviceSet::webapiSpectrumSettingsPutPatch( + bool force, + const QStringList& spectrumSettingsKeys, + SWGSDRangel::SWGGLSpectrum& response, // query + response + QString& errorMessage) +{ + return m_spectrumVis->webapiSpectrumSettingsPutPatch(force, spectrumSettingsKeys, response, errorMessage); +} + +int DeviceSet::webapiSpectrumServerGet(SWGSDRangel::SWGSpectrumServer& response, QString& errorMessage) const +{ + return m_spectrumVis->webapiSpectrumServerGet(response, errorMessage); +} + +int DeviceSet::webapiSpectrumServerPost(SWGSDRangel::SWGSuccessResponse& response, QString& errorMessage) +{ + return m_spectrumVis->webapiSpectrumServerPost(response, errorMessage); +} + +int DeviceSet::webapiSpectrumServerDelete(SWGSDRangel::SWGSuccessResponse& response, QString& errorMessage) +{ + return m_spectrumVis->webapiSpectrumServerDelete(response, errorMessage); +} diff --git a/sdrsrv/device/deviceset.h b/sdrsrv/device/deviceset.h index 73b54d6c2..f80195593 100644 --- a/sdrsrv/device/deviceset.h +++ b/sdrsrv/device/deviceset.h @@ -27,6 +27,13 @@ class DSPDeviceMIMOEngine; class PluginAPI; class ChannelAPI; class Preset; +class SpectrumVis; + +namespace SWGSDRangel { + class SWGGLSpectrum; + class SWGSpectrumServer; + class SWGSuccessResponse; +}; class DeviceSet { @@ -35,8 +42,9 @@ public: DSPDeviceSourceEngine *m_deviceSourceEngine; DSPDeviceSinkEngine *m_deviceSinkEngine; DSPDeviceMIMOEngine *m_deviceMIMOEngine; + SpectrumVis *m_spectrumVis; - DeviceSet(int tabIndex); + DeviceSet(int tabIndex, int deviceType); ~DeviceSet(); int getNumberOfChannels() const { return m_channelInstanceRegistrations.size(); } @@ -58,6 +66,17 @@ public: void loadMIMOChannelSettings(const Preset* preset, PluginAPI *pluginAPI); void saveMIMOChannelSettings(Preset* preset); + // REST API + int webapiSpectrumSettingsGet(SWGSDRangel::SWGGLSpectrum& response, QString& errorMessage) const; + int webapiSpectrumSettingsPutPatch( + bool force, + const QStringList& spectrumSettingsKeys, + SWGSDRangel::SWGGLSpectrum& response, // query + response + QString& errorMessage); + int webapiSpectrumServerGet(SWGSDRangel::SWGSpectrumServer& response, QString& errorMessage) const; + int webapiSpectrumServerPost(SWGSDRangel::SWGSuccessResponse& response, QString& errorMessage); + int webapiSpectrumServerDelete(SWGSDRangel::SWGSuccessResponse& response, QString& errorMessage); + private: struct ChannelInstanceRegistration { diff --git a/sdrsrv/maincore.cpp b/sdrsrv/maincore.cpp index d2c5fa93e..3d26681b9 100644 --- a/sdrsrv/maincore.cpp +++ b/sdrsrv/maincore.cpp @@ -25,6 +25,7 @@ #include "dsp/dspdevicesourceengine.h" #include "dsp/dspdevicesinkengine.h" #include "dsp/dspdevicemimoengine.h" +#include "dsp/spectrumvis.h" #include "device/deviceapi.h" #include "device/deviceset.h" #include "device/deviceenumerator.h" @@ -287,10 +288,11 @@ void MainCore::addSinkDevice() sprintf(uidCStr, "UID:%d", dspDeviceSinkEngineUID); int deviceTabIndex = m_deviceSets.size(); - m_deviceSets.push_back(new DeviceSet(deviceTabIndex)); + m_deviceSets.push_back(new DeviceSet(deviceTabIndex, 1)); m_deviceSets.back()->m_deviceSourceEngine = nullptr; m_deviceSets.back()->m_deviceSinkEngine = dspDeviceSinkEngine; m_deviceSets.back()->m_deviceMIMOEngine = nullptr; + dspDeviceSinkEngine->addSpectrumSink(m_deviceSets.back()->m_spectrumVis); char tabNameCStr[16]; sprintf(tabNameCStr, "T%d", deviceTabIndex); @@ -333,10 +335,11 @@ void MainCore::addSourceDevice() sprintf(uidCStr, "UID:%d", dspDeviceSourceEngineUID); int deviceTabIndex = m_deviceSets.size(); - m_deviceSets.push_back(new DeviceSet(deviceTabIndex)); + m_deviceSets.push_back(new DeviceSet(deviceTabIndex, 0)); m_deviceSets.back()->m_deviceSourceEngine = dspDeviceSourceEngine; m_deviceSets.back()->m_deviceSinkEngine = nullptr; m_deviceSets.back()->m_deviceMIMOEngine = nullptr; + dspDeviceSourceEngine->addSink(m_deviceSets.back()->m_spectrumVis); char tabNameCStr[16]; sprintf(tabNameCStr, "R%d", deviceTabIndex); @@ -378,10 +381,11 @@ void MainCore::addMIMODevice() sprintf(uidCStr, "UID:%d", dspDeviceMIMOEngineUID); int deviceTabIndex = m_deviceSets.size(); - m_deviceSets.push_back(new DeviceSet(deviceTabIndex)); + m_deviceSets.push_back(new DeviceSet(deviceTabIndex, 2)); m_deviceSets.back()->m_deviceSourceEngine = nullptr; m_deviceSets.back()->m_deviceSinkEngine = nullptr; m_deviceSets.back()->m_deviceMIMOEngine = dspDeviceMIMOEngine; + dspDeviceMIMOEngine->addSpectrumSink(m_deviceSets.back()->m_spectrumVis); char tabNameCStr[16]; sprintf(tabNameCStr, "M%d", deviceTabIndex); diff --git a/sdrsrv/webapi/webapiadaptersrv.cpp b/sdrsrv/webapi/webapiadaptersrv.cpp index b64e0d271..987c5e69d 100644 --- a/sdrsrv/webapi/webapiadaptersrv.cpp +++ b/sdrsrv/webapi/webapiadaptersrv.cpp @@ -1619,6 +1619,109 @@ int WebAPIAdapterSrv::devicesetDevicePut( } } +int WebAPIAdapterSrv::devicesetSpectrumSettingsGet( + int deviceSetIndex, + SWGSDRangel::SWGGLSpectrum& response, + SWGSDRangel::SWGErrorResponse& error) +{ + if ((deviceSetIndex >= 0) && (deviceSetIndex < (int) m_mainCore.m_deviceSets.size())) + { + const DeviceSet *deviceSet = m_mainCore.m_deviceSets[deviceSetIndex]; + return deviceSet->webapiSpectrumSettingsGet(response, *error.getMessage()); + } + else + { + error.init(); + *error.getMessage() = QString("There is no device set with index %1").arg(deviceSetIndex); + + return 404; + } +} + +int WebAPIAdapterSrv::devicesetSpectrumSettingsPutPatch( + int deviceSetIndex, + bool force, //!< true to force settings = put else patch + const QStringList& spectrumSettingsKeys, + SWGSDRangel::SWGGLSpectrum& response, + SWGSDRangel::SWGErrorResponse& error) +{ + if ((deviceSetIndex >= 0) && (deviceSetIndex < (int) m_mainCore.m_deviceSets.size())) + { + DeviceSet *deviceSet = m_mainCore.m_deviceSets[deviceSetIndex]; + return deviceSet->webapiSpectrumSettingsPutPatch(force, spectrumSettingsKeys, response, *error.getMessage()); + } + else + { + error.init(); + *error.getMessage() = QString("There is no device set with index %1").arg(deviceSetIndex); + + return 404; + } +} + +int WebAPIAdapterSrv::devicesetSpectrumServerGet( + int deviceSetIndex, + SWGSDRangel::SWGSpectrumServer& response, + SWGSDRangel::SWGErrorResponse& error) +{ + if ((deviceSetIndex >= 0) && (deviceSetIndex < (int) m_mainCore.m_deviceSets.size())) + { + const DeviceSet *deviceSet = m_mainCore.m_deviceSets[deviceSetIndex]; + deviceSet->webapiSpectrumServerGet(response, *error.getMessage()); + + return 200; + } + else + { + error.init(); + *error.getMessage() = QString("There is no device set with index %1").arg(deviceSetIndex); + + return 404; + } +} + +int WebAPIAdapterSrv::devicesetSpectrumServerPost( + int deviceSetIndex, + SWGSDRangel::SWGSuccessResponse& response, + SWGSDRangel::SWGErrorResponse& error) +{ + if ((deviceSetIndex >= 0) && (deviceSetIndex < (int) m_mainCore.m_deviceSets.size())) + { + DeviceSet *deviceSet = m_mainCore.m_deviceSets[deviceSetIndex]; + deviceSet->webapiSpectrumServerPost(response, *error.getMessage()); + + return 200; + } + else + { + error.init(); + *error.getMessage() = QString("There is no device set with index %1").arg(deviceSetIndex); + + return 404; + } +} + +int WebAPIAdapterSrv::devicesetSpectrumServerDelete( + int deviceSetIndex, + SWGSDRangel::SWGSuccessResponse& response, + SWGSDRangel::SWGErrorResponse& error) +{ + if ((deviceSetIndex >= 0) && (deviceSetIndex < (int) m_mainCore.m_deviceSets.size())) + { + DeviceSet *deviceSet = m_mainCore.m_deviceSets[deviceSetIndex]; + deviceSet->webapiSpectrumServerDelete(response, *error.getMessage()); + + return 200; + } + else + { + error.init(); + *error.getMessage() = QString("There is no device set with index %1").arg(deviceSetIndex); + + return 404; + } +} + int WebAPIAdapterSrv::devicesetDeviceSettingsGet( int deviceSetIndex, SWGSDRangel::SWGDeviceSettings& response, diff --git a/sdrsrv/webapi/webapiadaptersrv.h b/sdrsrv/webapi/webapiadaptersrv.h index a70f4bf27..d7fe1175e 100644 --- a/sdrsrv/webapi/webapiadaptersrv.h +++ b/sdrsrv/webapi/webapiadaptersrv.h @@ -228,6 +228,33 @@ public: SWGSDRangel::SWGDeviceListItem& response, SWGSDRangel::SWGErrorResponse& error); + virtual int devicesetSpectrumSettingsGet( + int deviceSetIndex, + SWGSDRangel::SWGGLSpectrum& response, + SWGSDRangel::SWGErrorResponse& error); + + virtual int devicesetSpectrumSettingsPutPatch( + int deviceSetIndex, + bool force, //!< true to force settings = put else patch + const QStringList& spectrumSettingsKeys, + SWGSDRangel::SWGGLSpectrum& response, + SWGSDRangel::SWGErrorResponse& error); + + virtual int devicesetSpectrumServerGet( + int deviceSetIndex, + SWGSDRangel::SWGSpectrumServer& response, + SWGSDRangel::SWGErrorResponse& error); + + virtual int devicesetSpectrumServerPost( + int deviceSetIndex, + SWGSDRangel::SWGSuccessResponse& response, + SWGSDRangel::SWGErrorResponse& error); + + virtual int devicesetSpectrumServerDelete( + int deviceSetIndex, + SWGSDRangel::SWGSuccessResponse& response, + SWGSDRangel::SWGErrorResponse& error); + virtual int devicesetDeviceSettingsGet( int deviceSetIndex, SWGSDRangel::SWGDeviceSettings& response,