From 9c581107e80f65f2b0dfe7a069195e8ff1dbae98 Mon Sep 17 00:00:00 2001 From: f4exb Date: Fri, 8 Dec 2017 17:12:33 +0100 Subject: [PATCH] Web API: RTLSDR run implementation --- plugins/samplesource/rtlsdr/rtlsdrinput.cpp | 24 +++++++ plugins/samplesource/rtlsdr/rtlsdrinput.h | 5 ++ sdrbase/dsp/devicesamplesink.h | 6 ++ sdrbase/dsp/devicesamplesource.h | 6 ++ sdrbase/webapi/webapirequestmapper.cpp | 52 +++++++++++++++ sdrbase/webapi/webapirequestmapper.h | 1 + sdrgui/webapi/webapiadaptergui.cpp | 70 +++++++++++++++++++++ sdrgui/webapi/webapiadaptergui.h | 10 +++ 8 files changed, 174 insertions(+) diff --git a/plugins/samplesource/rtlsdr/rtlsdrinput.cpp b/plugins/samplesource/rtlsdr/rtlsdrinput.cpp index 867e26e23..8caf36c33 100644 --- a/plugins/samplesource/rtlsdr/rtlsdrinput.cpp +++ b/plugins/samplesource/rtlsdr/rtlsdrinput.cpp @@ -21,6 +21,7 @@ #include "SWGDeviceSettings.h" #include "SWGRtlSdrSettings.h" +#include "SWGDeviceState.h" #include "rtlsdrinput.h" #include "device/devicesourceapi.h" @@ -524,3 +525,26 @@ int RTLSDRInput::webapiSettingsPutPatch( return 200; } + +int RTLSDRInput::webapiRun( + bool run, + SWGSDRangel::SWGDeviceState& response, + QString& errorMessage __attribute__((unused))) +{ + if (run) + { + if (m_deviceAPI->initAcquisition()) + { + m_deviceAPI->startAcquisition(); + DSPEngine::instance()->startAudioOutput(); + } + } + else + { + m_deviceAPI->stopAcquisition(); + DSPEngine::instance()->stopAudioOutput(); + } + + m_deviceAPI->getDeviceEngineStateStr(*response.getState()); + return 200; +} diff --git a/plugins/samplesource/rtlsdr/rtlsdrinput.h b/plugins/samplesource/rtlsdr/rtlsdrinput.h index ea75fd307..398c4df50 100644 --- a/plugins/samplesource/rtlsdr/rtlsdrinput.h +++ b/plugins/samplesource/rtlsdr/rtlsdrinput.h @@ -115,6 +115,11 @@ public: SWGSDRangel::SWGDeviceSettings& response, // query + response QString& errorMessage); + virtual int webapiRun( + bool run, + SWGSDRangel::SWGDeviceState& response, + QString& errorMessage); + const std::vector& getGains() const { return m_gains; } void set_ds_mode(int on); diff --git a/sdrbase/dsp/devicesamplesink.h b/sdrbase/dsp/devicesamplesink.h index 0fe264f27..b837bfffb 100644 --- a/sdrbase/dsp/devicesamplesink.h +++ b/sdrbase/dsp/devicesamplesink.h @@ -28,6 +28,7 @@ namespace SWGSDRangel { class SWGDeviceSettings; + class SWGDeviceState; } class SDRANGEL_API DeviceSampleSink : public QObject { @@ -57,6 +58,11 @@ public: QString& errorMessage) { errorMessage = "Not implemented"; return 501; } + virtual int webapiRun(bool run __attribute__((unused)), + SWGSDRangel::SWGDeviceState& 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 769233bf1..03df4ac0a 100644 --- a/sdrbase/dsp/devicesamplesource.h +++ b/sdrbase/dsp/devicesamplesource.h @@ -28,6 +28,7 @@ namespace SWGSDRangel { class SWGDeviceSettings; + class SWGDeviceState; } class SDRANGEL_API DeviceSampleSource : public QObject { @@ -57,6 +58,11 @@ public: QString& errorMessage) { errorMessage = "Not implemented"; return 501; } + virtual int webapiRun(bool run __attribute__((unused)), + SWGSDRangel::SWGDeviceState& 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; } diff --git a/sdrbase/webapi/webapirequestmapper.cpp b/sdrbase/webapi/webapirequestmapper.cpp index 313494f0f..677e36c09 100644 --- a/sdrbase/webapi/webapirequestmapper.cpp +++ b/sdrbase/webapi/webapirequestmapper.cpp @@ -35,6 +35,7 @@ #include "SWGPresetTransfer.h" #include "SWGPresetIdentifier.h" #include "SWGDeviceSettings.h" +#include "SWGDeviceState.h" #include "SWGErrorResponse.h" WebAPIRequestMapper::WebAPIRequestMapper(QObject* parent) : @@ -92,6 +93,8 @@ void WebAPIRequestMapper::service(qtwebapp::HttpRequest& request, qtwebapp::Http devicesetDeviceService(std::string(desc_match[1]), request, response); } else if (std::regex_match(pathStr, desc_match, WebAPIAdapterInterface::devicesetDeviceSettingsURLRe)) { devicesetDeviceSettingsService(std::string(desc_match[1]), request, response); + } else if (std::regex_match(pathStr, desc_match, WebAPIAdapterInterface::devicesetDeviceRunURLRe)) { + devicesetDeviceRunService(std::string(desc_match[1]), request, response); } else { @@ -697,6 +700,55 @@ void WebAPIRequestMapper::devicesetDeviceSettingsService(const std::string& inde } } +void WebAPIRequestMapper::devicesetDeviceRunService(const std::string& indexStr, qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response) +{ + SWGSDRangel::SWGErrorResponse errorResponse; + + try + { + int deviceSetIndex = boost::lexical_cast(indexStr); + + if (request.getMethod() == "POST") + { + SWGSDRangel::SWGDeviceState normalResponse; + int status = m_adapter->devicesetDeviceRunPost(deviceSetIndex, normalResponse, errorResponse); + + response.setStatus(status); + + if (status == 200) { + response.write(normalResponse.asJson().toUtf8()); + } else { + response.write(errorResponse.asJson().toUtf8()); + } + } + else if (request.getMethod() == "DELETE") + { + SWGSDRangel::SWGDeviceState normalResponse; + int status = m_adapter->devicesetDeviceRunDelete(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"); + response.write("Invalid HTTP method"); + } + } + catch (const boost::bad_lexical_cast &e) + { + errorResponse.init(); + *errorResponse.getMessage() = "Wrong integer conversion on device set index"; + response.setStatus(400,"Invalid data"); + response.write(errorResponse.asJson().toUtf8()); + } +} + bool WebAPIRequestMapper::parseJsonBody(QString& jsonStr, QJsonObject& jsonObject, qtwebapp::HttpResponse& response) { SWGSDRangel::SWGErrorResponse errorResponse; diff --git a/sdrbase/webapi/webapirequestmapper.h b/sdrbase/webapi/webapirequestmapper.h index b4d93c1c9..f677fb3ab 100644 --- a/sdrbase/webapi/webapirequestmapper.h +++ b/sdrbase/webapi/webapirequestmapper.h @@ -58,6 +58,7 @@ private: void devicesetService(const std::string& indexStr, qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response); void devicesetDeviceService(const std::string& indexStr, qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response); void devicesetDeviceSettingsService(const std::string& indexStr, qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response); + void devicesetDeviceRunService(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 24d1e57b1..4eeea05f5 100644 --- a/sdrgui/webapi/webapiadaptergui.cpp +++ b/sdrgui/webapi/webapiadaptergui.cpp @@ -51,6 +51,7 @@ #include "SWGPresetTransfer.h" #include "SWGPresetIdentifier.h" #include "SWGDeviceSettings.h" +#include "SWGDeviceState.h" #include "SWGErrorResponse.h" #include "webapiadaptergui.h" @@ -783,6 +784,75 @@ int WebAPIAdapterGUI::devicesetDeviceSettingsPutPatch( } } +int WebAPIAdapterGUI::devicesetDeviceRunPost( + int deviceSetIndex, + SWGSDRangel::SWGDeviceState& response, + SWGSDRangel::SWGErrorResponse& error) +{ + if ((deviceSetIndex >= 0) && (deviceSetIndex < (int) m_mainWindow.m_deviceUIs.size())) + { + DeviceUISet *deviceSet = m_mainWindow.m_deviceUIs[deviceSetIndex]; + + if (deviceSet->m_deviceSourceEngine) // Rx + { + DeviceSampleSource *source = deviceSet->m_deviceSourceAPI->getSampleSource(); + return source->webapiRun(true, response, *error.getMessage()); + } + else if (deviceSet->m_deviceSinkEngine) // Tx + { + DeviceSampleSink *sink = deviceSet->m_deviceSinkAPI->getSampleSink(); + return sink->webapiRun(true, response, *error.getMessage()); + } + else + { + *error.getMessage() = QString("DeviceSet error"); + return 500; + } + } + else + { + error.init(); + *error.getMessage() = QString("There is no device set with index %1").arg(deviceSetIndex); + + return 404; + } +} + +int WebAPIAdapterGUI::devicesetDeviceRunDelete( + int deviceSetIndex, + SWGSDRangel::SWGDeviceState& response, + SWGSDRangel::SWGErrorResponse& error) +{ + if ((deviceSetIndex >= 0) && (deviceSetIndex < (int) m_mainWindow.m_deviceUIs.size())) + { + DeviceUISet *deviceSet = m_mainWindow.m_deviceUIs[deviceSetIndex]; + + if (deviceSet->m_deviceSourceEngine) // Rx + { + DeviceSampleSource *source = deviceSet->m_deviceSourceAPI->getSampleSource(); + return source->webapiRun(false, response, *error.getMessage()); + } + else if (deviceSet->m_deviceSinkEngine) // Tx + { + DeviceSampleSink *sink = deviceSet->m_deviceSinkAPI->getSampleSink(); + return sink->webapiRun(false, response, *error.getMessage()); + } + else + { + *error.getMessage() = QString("DeviceSet error"); + return 500; + } + } + 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 d3ecefc8c..74e2d603e 100644 --- a/sdrgui/webapi/webapiadaptergui.h +++ b/sdrgui/webapi/webapiadaptergui.h @@ -131,6 +131,16 @@ public: SWGSDRangel::SWGDeviceSettings& response, SWGSDRangel::SWGErrorResponse& error); + virtual int devicesetDeviceRunPost( + int deviceSetIndex, + SWGSDRangel::SWGDeviceState& response, + SWGSDRangel::SWGErrorResponse& error); + + virtual int devicesetDeviceRunDelete( + int deviceSetIndex, + SWGSDRangel::SWGDeviceState& response, + SWGSDRangel::SWGErrorResponse& error); + private: MainWindow& m_mainWindow;