diff --git a/plugins/samplesource/rtlsdr/rtlsdr.pro b/plugins/samplesource/rtlsdr/rtlsdr.pro index da86b8ad7..a2e403e3d 100644 --- a/plugins/samplesource/rtlsdr/rtlsdr.pro +++ b/plugins/samplesource/rtlsdr/rtlsdr.pro @@ -22,6 +22,7 @@ CONFIG(MINGW64):LIBRTLSDRSRC = "D:\softs\librtlsdr" INCLUDEPATH += $$PWD INCLUDEPATH += ../../../sdrbase INCLUDEPATH += ../../../sdrgui +INCLUDEPATH += ../../../swagger/sdrangel/code/qt5/client !macx:INCLUDEPATH += $$LIBRTLSDRSRC/include macx:INCLUDEPATH += /opt/local/include @@ -44,6 +45,7 @@ FORMS += rtlsdrgui.ui LIBS += -L../../../sdrbase/$${build_subdir} -lsdrbase LIBS += -L../../../sdrgui/$${build_subdir} -lsdrgui +LIBS += -L../../../swagger/$${build_subdir} -lswagger !macx:LIBS += -L../../../librtlsdr/$${build_subdir} -llibrtlsdr macx:LIBS += -L/opt/local/lib -lrtlsdr diff --git a/sdrbase/dsp/devicesamplesink.h b/sdrbase/dsp/devicesamplesink.h index 33fdac6d9..0fe264f27 100644 --- a/sdrbase/dsp/devicesamplesink.h +++ b/sdrbase/dsp/devicesamplesink.h @@ -51,6 +51,12 @@ public: QString& errorMessage) { errorMessage = "Not implemented"; return 501; } + virtual int webapiSettingsPutPatch( + bool force __attribute__((unused)), //!< true to force settings = put + SWGSDRangel::SWGDeviceSettings& 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 6892eb506..769233bf1 100644 --- a/sdrbase/dsp/devicesamplesource.h +++ b/sdrbase/dsp/devicesamplesource.h @@ -51,6 +51,12 @@ public: QString& errorMessage) { errorMessage = "Not implemented"; return 501; } + virtual int webapiSettingsPutPatch( + bool force __attribute__((unused)), //!< true to force settings = put + SWGSDRangel::SWGDeviceSettings& 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/webapiadapterinterface.h b/sdrbase/webapi/webapiadapterinterface.h index 6212a515c..22d69212a 100644 --- a/sdrbase/webapi/webapiadapterinterface.h +++ b/sdrbase/webapi/webapiadapterinterface.h @@ -248,21 +248,12 @@ public: { return 501; } /** - * Handler of /sdrangel/deviceset/{devicesetIndex}/device/settings (PUT) swagger/sdrangel/code/html2/index.html#api-Default-instanceChannels + * Handler of /sdrangel/deviceset/{devicesetIndex}/device/settings (PUT, PATCH) swagger/sdrangel/code/html2/index.html#api-Default-instanceChannels * returns the Http status code (default 501: not implemented) */ - virtual int devicesetDeviceSettingsPut( - int deviceSetIndex __attribute__((unused)), - SWGSDRangel::SWGDeviceSettings& response __attribute__((unused)), - SWGSDRangel::SWGErrorResponse& error __attribute__((unused))) - { return 501; } - - /** - * Handler of /sdrangel/deviceset/{devicesetIndex}/device/settings (PATCH) swagger/sdrangel/code/html2/index.html#api-Default-instanceChannels - * returns the Http status code (default 501: not implemented) - */ - virtual int devicesetDeviceSettingsPatch( + virtual int devicesetDeviceSettingsPutPatch( int deviceSetIndex __attribute__((unused)), + bool force __attribute__((unused)), //!< true to force settings = put else patch SWGSDRangel::SWGDeviceSettings& response __attribute__((unused)), SWGSDRangel::SWGErrorResponse& error __attribute__((unused))) { return 501; } diff --git a/sdrbase/webapi/webapirequestmapper.cpp b/sdrbase/webapi/webapirequestmapper.cpp index 4ca0b997b..08e8811db 100644 --- a/sdrbase/webapi/webapirequestmapper.cpp +++ b/sdrbase/webapi/webapirequestmapper.cpp @@ -626,11 +626,39 @@ void WebAPIRequestMapper::devicesetDeviceSettingsService(const std::string& inde { int deviceSetIndex = boost::lexical_cast(indexStr); - if (request.getMethod() == "PUT") - { - } - else if (request.getMethod() == "PATCH") + if ((request.getMethod() == "PUT") || (request.getMethod() == "PATCH")) { + QString jsonStr = request.getBody(); + + if (parseJsonBody(jsonStr, response)) + { + SWGSDRangel::SWGDeviceSettings normalResponse; + resetDeviceSettings(normalResponse); + normalResponse.fromJson(jsonStr); + + if (validateDeviceSettings(normalResponse)) + { + int status = m_adapter->devicesetDeviceSettingsPutPatch( + deviceSetIndex, + (request.getMethod() == "PUT"), // force settings on PUT + normalResponse, + errorResponse); + response.setStatus(status); + + if (status == 200) { + response.write(normalResponse.asJson().toUtf8()); + } else { + response.write(errorResponse.asJson().toUtf8()); + } + } + else + { + response.setStatus(400,"Invalid JSON request"); + errorResponse.init(); + *errorResponse.getMessage() = "Invalid JSON request"; + response.write(errorResponse.asJson().toUtf8()); + } + } } else if (request.getMethod() == "GET") { @@ -709,6 +737,30 @@ bool WebAPIRequestMapper::validatePresetIdentifer(SWGSDRangel::SWGPresetIdentifi return (presetIdentifier.getGroupName() && presetIdentifier.getName() && presetIdentifier.getType()); } +bool WebAPIRequestMapper::validateDeviceSettings(SWGSDRangel::SWGDeviceSettings& deviceSettings) +{ + QString *deviceHwType = deviceSettings.getDeviceHwType(); + + if (deviceHwType == 0) + { + return false; + } + else + { + if (*deviceHwType == "FileSource") { + return deviceSettings.getFileSourceSettings() != 0; + } else if (*deviceHwType == "RTLSDR") { + return deviceSettings.getRtlSdrSettings() != 0; + } else if (*deviceHwType == "LimeSDR") { + if (deviceSettings.getTx() == 0) { + return deviceSettings.getLimeSdrInputSettings() != 0; + } else { + return deviceSettings.getLimeSdrOutputSettings() != 0; + } + } + } +} + void WebAPIRequestMapper::resetDeviceSettings(SWGSDRangel::SWGDeviceSettings& deviceSettings) { deviceSettings.cleanup(); diff --git a/sdrbase/webapi/webapirequestmapper.h b/sdrbase/webapi/webapirequestmapper.h index 5f52b0bf5..f898d42a1 100644 --- a/sdrbase/webapi/webapirequestmapper.h +++ b/sdrbase/webapi/webapirequestmapper.h @@ -61,6 +61,7 @@ private: bool validatePresetTransfer(SWGSDRangel::SWGPresetTransfer& presetTransfer); bool validatePresetIdentifer(SWGSDRangel::SWGPresetIdentifier& presetIdentifier); + bool validateDeviceSettings(SWGSDRangel::SWGDeviceSettings& deviceSettings); bool parseJsonBody(QString& jsonStr, qtwebapp::HttpResponse& response); diff --git a/sdrgui/webapi/webapiadaptergui.cpp b/sdrgui/webapi/webapiadaptergui.cpp index b882d8722..24d1e57b1 100644 --- a/sdrgui/webapi/webapiadaptergui.cpp +++ b/sdrgui/webapi/webapiadaptergui.cpp @@ -732,6 +732,57 @@ int WebAPIAdapterGUI::devicesetDeviceSettingsGet( } } +int WebAPIAdapterGUI::devicesetDeviceSettingsPutPatch( + int deviceSetIndex, + bool force, + SWGSDRangel::SWGDeviceSettings& 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 + { + if ((response.getTx() != 0) || (deviceSet->m_deviceSourceAPI->getHardwareId() != *response.getDeviceHwType())) + { + *error.getMessage() = QString("Device mismatch. Found %1 input").arg(deviceSet->m_deviceSourceAPI->getHardwareId()); + return 400; + } + else + { + DeviceSampleSource *source = deviceSet->m_deviceSourceAPI->getSampleSource(); + return source->webapiSettingsPutPatch(force, response, *error.getMessage()); + } + } + else if (deviceSet->m_deviceSinkEngine) // Tx + { + if ((response.getTx() == 0) || (deviceSet->m_deviceSourceAPI->getHardwareId() != *response.getDeviceHwType())) + { + *error.getMessage() = QString("Device mismatch. Found %1 output").arg(deviceSet->m_deviceSourceAPI->getHardwareId()); + return 400; + } + else + { + DeviceSampleSink *sink = deviceSet->m_deviceSinkAPI->getSampleSink(); + return sink->webapiSettingsPutPatch(force, 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 f2fdd5799..d3ecefc8c 100644 --- a/sdrgui/webapi/webapiadaptergui.h +++ b/sdrgui/webapi/webapiadaptergui.h @@ -125,6 +125,12 @@ public: SWGSDRangel::SWGDeviceSettings& response, SWGSDRangel::SWGErrorResponse& error); + virtual int devicesetDeviceSettingsPutPatch( + int deviceSetIndex, + bool force, + SWGSDRangel::SWGDeviceSettings& response, + SWGSDRangel::SWGErrorResponse& error); + private: MainWindow& m_mainWindow;