From 7dafae3fa10349d9f262942a0c7de1e0dc6d7362 Mon Sep 17 00:00:00 2001 From: f4exb Date: Mon, 4 Dec 2017 18:22:25 +0100 Subject: [PATCH] Web API: implement devicesetDeviceGet (1) --- sdrbase/dsp/devicesamplesink.h | 5 + sdrbase/dsp/devicesamplesource.h | 121 ++++++++++++------------ sdrbase/webapi/webapiadapterinterface.h | 12 ++- sdrbase/webapi/webapirequestmapper.cpp | 20 +++- sdrbase/webapi/webapirequestmapper.h | 4 +- sdrgui/webapi/webapiadaptergui.cpp | 42 ++++++++ sdrgui/webapi/webapiadaptergui.h | 5 + 7 files changed, 144 insertions(+), 65 deletions(-) diff --git a/sdrbase/dsp/devicesamplesink.h b/sdrbase/dsp/devicesamplesink.h index a32262ba8..2d33515e4 100644 --- a/sdrbase/dsp/devicesamplesink.h +++ b/sdrbase/dsp/devicesamplesink.h @@ -41,6 +41,11 @@ public: virtual bool handleMessage(const Message& message) = 0; + virtual int webapiSettingsGet( + SWGSDRangel::SWGObject *response __attribute__((unused)), + QString& errorMessage) + { errorMessage = "Not implemented"; return 501; } + MessageQueue *getInputMessageQueue() { return &m_inputMessageQueue; } void setMessageQueueToGUI(MessageQueue *queue) { m_guiMessageQueue = queue; } MessageQueue *getMessageQueueToGUI() { return m_guiMessageQueue; } diff --git a/sdrbase/dsp/devicesamplesource.h b/sdrbase/dsp/devicesamplesource.h index a604c13c2..f44c7272e 100644 --- a/sdrbase/dsp/devicesamplesource.h +++ b/sdrbase/dsp/devicesamplesource.h @@ -1,58 +1,63 @@ -/////////////////////////////////////////////////////////////////////////////////// -// Copyright (C) 2015 F4EXB // -// written by Edouard Griffiths // -// // -// 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 // -// // -// 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 . // -/////////////////////////////////////////////////////////////////////////////////// - -#ifndef INCLUDE_SAMPLESOURCE_H -#define INCLUDE_SAMPLESOURCE_H - -#include - -#include "samplesinkfifo.h" -#include "util/message.h" -#include "util/messagequeue.h" -#include "util/export.h" - -class SDRANGEL_API DeviceSampleSource : public QObject { - Q_OBJECT -public: - DeviceSampleSource(); - virtual ~DeviceSampleSource(); - virtual void destroy() = 0; - - virtual bool start() = 0; - virtual void stop() = 0; - - virtual const QString& getDeviceDescription() const = 0; - virtual int getSampleRate() const = 0; //!< Sample rate exposed by the source - virtual quint64 getCenterFrequency() const = 0; //!< Center frequency exposed by the source - - virtual bool handleMessage(const Message& message) = 0; - - MessageQueue *getInputMessageQueue() { return &m_inputMessageQueue; } - virtual void setMessageQueueToGUI(MessageQueue *queue) { m_guiMessageQueue = queue; } - MessageQueue *getMessageQueueToGUI() { return m_guiMessageQueue; } - SampleSinkFifo* getSampleFifo() { return &m_sampleFifo; } - -protected slots: - void handleInputMessages(); - -protected: - SampleSinkFifo m_sampleFifo; - MessageQueue m_inputMessageQueue; //!< Input queue to the source - MessageQueue *m_guiMessageQueue; //!< Input message queue to the GUI -}; - -#endif // INCLUDE_SAMPLESOURCE_H +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2015 F4EXB // +// written by Edouard Griffiths // +// // +// 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 // +// // +// 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 . // +/////////////////////////////////////////////////////////////////////////////////// + +#ifndef INCLUDE_SAMPLESOURCE_H +#define INCLUDE_SAMPLESOURCE_H + +#include + +#include "samplesinkfifo.h" +#include "util/message.h" +#include "util/messagequeue.h" +#include "util/export.h" + +class SDRANGEL_API DeviceSampleSource : public QObject { + Q_OBJECT +public: + DeviceSampleSource(); + virtual ~DeviceSampleSource(); + virtual void destroy() = 0; + + virtual bool start() = 0; + virtual void stop() = 0; + + virtual const QString& getDeviceDescription() const = 0; + virtual int getSampleRate() const = 0; //!< Sample rate exposed by the source + virtual quint64 getCenterFrequency() const = 0; //!< Center frequency exposed by the source + + virtual bool handleMessage(const Message& message) = 0; + + virtual int webapiSettingsGet( + SWGSDRangel::SWGObject *response __attribute__((unused)), + QString& errorMessage) + { errorMessage = "Not implemented"; return 501; } + + MessageQueue *getInputMessageQueue() { return &m_inputMessageQueue; } + virtual void setMessageQueueToGUI(MessageQueue *queue) { m_guiMessageQueue = queue; } + MessageQueue *getMessageQueueToGUI() { return m_guiMessageQueue; } + SampleSinkFifo* getSampleFifo() { return &m_sampleFifo; } + +protected slots: + void handleInputMessages(); + +protected: + SampleSinkFifo m_sampleFifo; + MessageQueue m_inputMessageQueue; //!< Input queue to the source + MessageQueue *m_guiMessageQueue; //!< Input message queue to the GUI +}; + +#endif // INCLUDE_SAMPLESOURCE_H diff --git a/sdrbase/webapi/webapiadapterinterface.h b/sdrbase/webapi/webapiadapterinterface.h index 5a697fc73..3b16545ab 100644 --- a/sdrbase/webapi/webapiadapterinterface.h +++ b/sdrbase/webapi/webapiadapterinterface.h @@ -226,7 +226,7 @@ public: { return 501; } /** - * Handler of /sdrangel/devicesets (DELETE) swagger/sdrangel/code/html2/index.html#api-Default-instanceChannels + * Handler of /sdrangel/deviceset/{devicesetIndex}/device (PUT) swagger/sdrangel/code/html2/index.html#api-Default-instanceChannels * returns the Http status code (default 501: not implemented) */ virtual int devicesetDevicePut( @@ -235,6 +235,16 @@ public: SWGSDRangel::SWGErrorResponse& error __attribute__((unused))) { return 501; } + /** + * Handler of /sdrangel/deviceset/{devicesetIndex}/device (GET) swagger/sdrangel/code/html2/index.html#api-Default-instanceChannels + * returns the Http status code (default 501: not implemented) + */ + virtual int devicesetDeviceGet( + int deviceSetIndex __attribute__((unused)), + SWGSDRangel::SWGDeviceSettings& response __attribute__((unused)), + SWGSDRangel::SWGErrorResponse& error __attribute__((unused))) + { return 501; } + static QString instanceSummaryURL; static QString instanceDevicesURL; static QString instanceChannelsURL; diff --git a/sdrbase/webapi/webapirequestmapper.cpp b/sdrbase/webapi/webapirequestmapper.cpp index 87e0046f0..2aebe28fe 100644 --- a/sdrbase/webapi/webapirequestmapper.cpp +++ b/sdrbase/webapi/webapirequestmapper.cpp @@ -86,9 +86,9 @@ void WebAPIRequestMapper::service(qtwebapp::HttpRequest& request, qtwebapp::Http std::string pathStr(path.constData(), path.length()); if (std::regex_match(pathStr, desc_match, WebAPIAdapterInterface::devicesetURLRe)) { - deviceset(std::string(desc_match[1]), request, response); + devicesetService(std::string(desc_match[1]), request, response); } else if (std::regex_match(pathStr, desc_match, WebAPIAdapterInterface::devicesetDeviceURLRe)) { - devicesetDevice(std::string(desc_match[1]), request, response); + devicesetDeviceService(std::string(desc_match[1]), request, response); } else { @@ -539,7 +539,7 @@ void WebAPIRequestMapper::instanceDeviceSetsService(qtwebapp::HttpRequest& reque } } -void WebAPIRequestMapper::deviceset(const std::string& indexStr, qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response) +void WebAPIRequestMapper::devicesetService(const std::string& indexStr, qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response) { SWGSDRangel::SWGErrorResponse errorResponse; @@ -573,7 +573,7 @@ void WebAPIRequestMapper::deviceset(const std::string& indexStr, qtwebapp::HttpR } } -void WebAPIRequestMapper::devicesetDevice(const std::string& indexStr, qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response) +void WebAPIRequestMapper::devicesetDeviceService(const std::string& indexStr, qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response) { SWGSDRangel::SWGErrorResponse errorResponse; @@ -600,6 +600,18 @@ void WebAPIRequestMapper::devicesetDevice(const std::string& indexStr, qtwebapp: } } } + else if (request.getMethod() == "GET") + { + SWGSDRangel::SWGDeviceSettings normalResponse; + int status = m_adapter->devicesetDeviceGet(deviceSetIndex, normalResponse, errorResponse); + response.setStatus(status); + + if (status == 200) { + response.write(normalResponse.asJson().toUtf8()); + } else { + response.write(errorResponse.asJson().toUtf8()); + } + } else { response.setStatus(405,"Invalid HTTP method"); diff --git a/sdrbase/webapi/webapirequestmapper.h b/sdrbase/webapi/webapirequestmapper.h index a5a872730..ad94a023f 100644 --- a/sdrbase/webapi/webapirequestmapper.h +++ b/sdrbase/webapi/webapirequestmapper.h @@ -55,8 +55,8 @@ private: void instancePresetService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response); void instanceDeviceSetsService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response); - void deviceset(const std::string& indexStr, qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response); - void devicesetDevice(const std::string& indexStr, qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response); + void devicesetService(const std::string& indexStr, qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response); + void devicesetDeviceService(const std::string& indexStr, qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response); bool validatePresetTransfer(SWGSDRangel::SWGPresetTransfer& presetTransfer); bool validatePresetIdentifer(SWGSDRangel::SWGPresetIdentifier& presetIdentifier); diff --git a/sdrgui/webapi/webapiadaptergui.cpp b/sdrgui/webapi/webapiadaptergui.cpp index 3bebfe184..56b7fdad7 100644 --- a/sdrgui/webapi/webapiadaptergui.cpp +++ b/sdrgui/webapi/webapiadaptergui.cpp @@ -693,6 +693,48 @@ int WebAPIAdapterGUI::devicesetDevicePut( } } +int WebAPIAdapterGUI::devicesetDeviceGet( + int deviceSetIndex, + SWGSDRangel::SWGDeviceSettings& response, + SWGSDRangel::SWGErrorResponse& error) +{ + if ((deviceSetIndex >= 0) && (m_mainWindow.m_deviceUIs < (int) m_mainWindow.m_deviceUIs.size())) + { + DeviceUISet *deviceSet = m_mainWindow.m_deviceUIs[deviceSetIndex]; + int tx = response.getTx(); + + if ((tx == 0) && (deviceSet->m_deviceSinkEngine)) + { + *error.getMessage() = QString("Device type (Rx) and device set type (Tx) mismatch"); + return 404; + } + + if ((tx != 0) && (deviceSet->m_deviceSourceEngine)) + { + *error.getMessage() = QString("Device type (Tx) and device set type (Rx) mismatch"); + return 404; + } + + if (tx == 0) // Rx + { + DeviceSampleSource *source = deviceSet->m_deviceSourceAPI->getSampleSource(); + return source->webapiSettingsGet(response.getData(), *error.getMessage()); + } + else // Tx + { + DeviceSampleSink *sink = deviceSet->m_deviceSinkAPI->getSampleSink(); + return sink->webapiSettingsGet(response.getData(), *error.getMessage()); + } + } + else + { + error.init(); + *error.getMessage() = QString("There is no device set with index %1").arg(deviceSetIndex); + + return 404; + } +} + void WebAPIAdapterGUI::getDeviceSetList(SWGSDRangel::SWGDeviceSetList* deviceSetList) { deviceSetList->init(); diff --git a/sdrgui/webapi/webapiadaptergui.h b/sdrgui/webapi/webapiadaptergui.h index e8f649850..9cd3cd49e 100644 --- a/sdrgui/webapi/webapiadaptergui.h +++ b/sdrgui/webapi/webapiadaptergui.h @@ -120,6 +120,11 @@ public: SWGSDRangel::SWGDeviceListItem& response, SWGSDRangel::SWGErrorResponse& error); + virtual int devicesetDeviceGet( + int deviceSetIndex, + SWGSDRangel::SWGDeviceSettings& response, + SWGSDRangel::SWGErrorResponse& error); + private: MainWindow& m_mainWindow;