From b011185e42cd1980bf5008c9adf19bd7b30d3c48 Mon Sep 17 00:00:00 2001 From: f4exb Date: Sat, 23 Dec 2017 22:33:30 +0100 Subject: [PATCH] Server: Web API: /sdrangel/deviceset/{deviceSetIndex}/channel/{channelIndex} DELETE --- sdrbase/webapi/webapiadapterinterface.h | 16 +++--- sdrbase/webapi/webapirequestmapper.cpp | 5 +- sdrsrv/device/deviceset.cpp | 20 +++++++ sdrsrv/device/deviceset.h | 4 ++ sdrsrv/maincore.cpp | 25 ++++++++- sdrsrv/maincore.h | 27 ++++++++++ sdrsrv/webapi/webapiadaptersrv.cpp | 71 +++++++++++++++++++++++-- sdrsrv/webapi/webapiadaptersrv.h | 6 +++ 8 files changed, 159 insertions(+), 15 deletions(-) diff --git a/sdrbase/webapi/webapiadapterinterface.h b/sdrbase/webapi/webapiadapterinterface.h index 47113db0e..f90b6d1d9 100644 --- a/sdrbase/webapi/webapiadapterinterface.h +++ b/sdrbase/webapi/webapiadapterinterface.h @@ -435,7 +435,7 @@ public: } /** - * Handler of /sdrangel/deviceset/{deviceSetIndex}/channel/{channelIndex}/settings (GET) swagger/sdrangel/code/html2/index.html#api-Default-instanceChannels + * Handler of /sdrangel/deviceset/{deviceSetIndex}/channel (POST) swagger/sdrangel/code/html2/index.html#api-Default-instanceChannels * returns the Http status code (default 501: not implemented) */ virtual int devicesetChannelPost( @@ -450,18 +450,18 @@ public: } /** - * Handler of /sdrangel/deviceset/{deviceSetIndex}/channel/{channelIndex}/settings (GET) swagger/sdrangel/code/html2/index.html#api-Default-instanceChannels + * Handler of /sdrangel/deviceset/{deviceSetIndex}/channel/{channelIndex} (DELETE) swagger/sdrangel/code/html2/index.html#api-Default-instanceChannels * returns the Http status code (default 501: not implemented) */ - virtual int devicesetChannelIndexDelete( + virtual int devicesetChannelDelete( int deviceSetIndex __attribute__((unused)), int channelIndex __attribute__((unused)), - SWGSDRangel::SWGChannelSettings& response __attribute__((unused)), - SWGSDRangel::SWGErrorResponse& error) + SWGSDRangel::SWGSuccessResponse& response __attribute__((unused)), + SWGSDRangel::SWGErrorResponse& error __attribute__((unused))) { - error.init(); - *error.getMessage() = QString("Function not implemented"); - return 501; + error.init(); + *error.getMessage() = QString("Function not implemented"); + return 501; } /** diff --git a/sdrbase/webapi/webapirequestmapper.cpp b/sdrbase/webapi/webapirequestmapper.cpp index 23d6f03ce..37c453791 100644 --- a/sdrbase/webapi/webapirequestmapper.cpp +++ b/sdrbase/webapi/webapirequestmapper.cpp @@ -1111,9 +1111,8 @@ void WebAPIRequestMapper::devicesetChannelIndexService( if (request.getMethod() == "DELETE") { - SWGSDRangel::SWGChannelSettings normalResponse; - resetChannelSettings(normalResponse); - int status = m_adapter->devicesetChannelIndexDelete(deviceSetIndex, channelIndex, normalResponse, errorResponse); + SWGSDRangel::SWGSuccessResponse normalResponse; + int status = m_adapter->devicesetChannelDelete(deviceSetIndex, channelIndex, normalResponse, errorResponse); response.setStatus(status); diff --git a/sdrsrv/device/deviceset.cpp b/sdrsrv/device/deviceset.cpp index 28428b744..ae58535ea 100644 --- a/sdrsrv/device/deviceset.cpp +++ b/sdrsrv/device/deviceset.cpp @@ -99,6 +99,26 @@ void DeviceSet::freeTxChannels() } } +void DeviceSet::deleteRxChannel(int channelIndex) +{ + if (channelIndex < m_rxChannelInstanceRegistrations.count()) + { + m_rxChannelInstanceRegistrations[channelIndex].m_channelSinkAPI->destroy(); + m_rxChannelInstanceRegistrations.removeAt(channelIndex); + renameRxChannelInstances(); + } +} + +void DeviceSet::deleteTxChannel(int channelIndex) +{ + if (channelIndex < m_txChannelInstanceRegistrations.count()) + { + m_txChannelInstanceRegistrations[channelIndex].m_channelSourceAPI->destroy(); + m_txChannelInstanceRegistrations.removeAt(channelIndex); + renameTxChannelInstances(); + } +} + void DeviceSet::addRxChannel(int selectedChannelIndex, PluginAPI *pluginAPI) { PluginAPI::ChannelRegistrations *channelRegistrations = pluginAPI->getRxChannelRegistrations(); // Available channel plugins diff --git a/sdrsrv/device/deviceset.h b/sdrsrv/device/deviceset.h index 5cdaec0f9..0eecab2d7 100644 --- a/sdrsrv/device/deviceset.h +++ b/sdrsrv/device/deviceset.h @@ -38,8 +38,12 @@ struct DeviceSet DeviceSet(int tabIndex); ~DeviceSet(); + int getNumberOfRxChannels() const { return m_rxChannelInstanceRegistrations.size(); } + int getNumberOfTxChannels() const { return m_txChannelInstanceRegistrations.size(); } void addRxChannel(int selectedChannelIndex, PluginAPI *pluginAPI); void addTxChannel(int selectedChannelIndex, PluginAPI *pluginAPI); + void deleteRxChannel(int channelIndex); + void deleteTxChannel(int channelIndex); void registerRxChannelInstance(const QString& channelName, ChannelSinkAPI* channelAPI); void registerTxChannelInstance(const QString& channelName, ChannelSourceAPI* channelAPI); void removeRxChannelInstance(ChannelSinkAPI* channelAPI); diff --git a/sdrsrv/maincore.cpp b/sdrsrv/maincore.cpp index c0f58ee64..12afea660 100644 --- a/sdrsrv/maincore.cpp +++ b/sdrsrv/maincore.cpp @@ -42,6 +42,7 @@ MESSAGE_CLASS_DEFINITION(MainCore::MsgAddDeviceSet, Message) MESSAGE_CLASS_DEFINITION(MainCore::MsgRemoveLastDeviceSet, Message) MESSAGE_CLASS_DEFINITION(MainCore::MsgSetDevice, Message) MESSAGE_CLASS_DEFINITION(MainCore::MsgAddChannel, Message) +MESSAGE_CLASS_DEFINITION(MainCore::MsgDeleteChannel, Message) MainCore *MainCore::m_instance = 0; @@ -161,7 +162,12 @@ bool MainCore::handleMessage(const Message& cmd) addChannel(notif.getDeviceSetIndex(), notif.getChannelRegistrationIndex()); return true; } - + else if (MsgDeleteChannel::match(cmd)) + { + MsgDeleteChannel& notif = (MsgDeleteChannel&) cmd; + deleteChannel(notif.getDeviceSetIndex(), notif.getChannelIndex()); + return true; + } else { return false; @@ -485,6 +491,23 @@ void MainCore::addChannel(int deviceSetIndex, int selectedChannelIndex) } } +void MainCore::deleteChannel(int deviceSetIndex, int channelIndex) +{ + if (deviceSetIndex >= 0) + { + DeviceSet *deviceSet = m_deviceSets[deviceSetIndex]; + + if (deviceSet->m_deviceSourceEngine) // source device => Rx channels + { + deviceSet->deleteRxChannel(channelIndex); + } + else if (deviceSet->m_deviceSinkEngine) // sink device => Tx channels + { + deviceSet->deleteTxChannel(channelIndex); + } + } +} + void MainCore::loadPresetSettings(const Preset* preset, int tabIndex) { qDebug("MainCore::loadPresetSettings: preset [%s | %s]", diff --git a/sdrsrv/maincore.h b/sdrsrv/maincore.h index 68ce77efa..daf5dce34 100644 --- a/sdrsrv/maincore.h +++ b/sdrsrv/maincore.h @@ -66,6 +66,7 @@ public: void changeSampleSource(int deviceSetIndex, int selectedDeviceIndex); void changeSampleSink(int deviceSetIndex, int selectedDeviceIndex); void addChannel(int deviceSetIndex, int selectedChannelIndex); + void deleteChannel(int deviceSetIndex, int channelIndex); friend class WebAPIAdapterSrv; @@ -244,6 +245,32 @@ private: { } }; + class MsgDeleteChannel : public Message { + MESSAGE_CLASS_DECLARATION + + public: + int getDeviceSetIndex() const { return m_deviceSetIndex; } + int getChannelIndex() const { return m_channelIndex; } + bool isTx() const { return m_tx; } + + static MsgDeleteChannel* create(int deviceSetIndex, int channelIndex, bool tx) + { + return new MsgDeleteChannel(deviceSetIndex, channelIndex, tx); + } + + private: + int m_deviceSetIndex; + int m_channelIndex; + bool m_tx; + + MsgDeleteChannel(int deviceSetIndex, int channelIndex, bool tx) : + Message(), + m_deviceSetIndex(deviceSetIndex), + m_channelIndex(channelIndex), + m_tx(tx) + { } + }; + static MainCore *m_instance; MainSettings m_settings; int m_masterTabIndex; diff --git a/sdrsrv/webapi/webapiadaptersrv.cpp b/sdrsrv/webapi/webapiadaptersrv.cpp index 44e7622a8..0da1c2cca 100644 --- a/sdrsrv/webapi/webapiadaptersrv.cpp +++ b/sdrsrv/webapi/webapiadaptersrv.cpp @@ -621,7 +621,7 @@ int WebAPIAdapterSrv::instancePresetPost( } else if (deviceSet->m_deviceSinkEngine) { // Tx deviceCenterFrequency = deviceSet->m_deviceSinkEngine->getSink()->getCenterFrequency(); } else { - *error.getMessage() = QString("Unknown device in device set (not Rx nor Tx)"); + *error.getMessage() = QString("Device set error"); return 500; } @@ -850,6 +850,7 @@ int WebAPIAdapterSrv::devicesetDeviceSettingsGet( } else { + error.init(); *error.getMessage() = QString("DeviceSet error"); return 500; } @@ -858,7 +859,6 @@ int WebAPIAdapterSrv::devicesetDeviceSettingsGet( { error.init(); *error.getMessage() = QString("There is no device set with index %1").arg(deviceSetIndex); - return 404; } } @@ -911,6 +911,7 @@ int WebAPIAdapterSrv::devicesetDeviceSettingsPutPatch( } else { + error.init(); *error.getMessage() = QString("DeviceSet error"); return 500; } @@ -919,7 +920,6 @@ int WebAPIAdapterSrv::devicesetDeviceSettingsPutPatch( { error.init(); *error.getMessage() = QString("There is no device set with index %1").arg(deviceSetIndex); - return 404; } } @@ -1015,6 +1015,71 @@ int WebAPIAdapterSrv::devicesetChannelPost( } } +int WebAPIAdapterSrv::devicesetChannelDelete( + int deviceSetIndex, + int channelIndex, + SWGSDRangel::SWGSuccessResponse& response, + SWGSDRangel::SWGErrorResponse& error) +{ + if ((deviceSetIndex >= 0) && (deviceSetIndex < (int) m_mainCore.m_deviceSets.size())) + { + DeviceSet *deviceSet = m_mainCore.m_deviceSets[deviceSetIndex]; + + if (deviceSet->m_deviceSourceEngine) // Rx + { + if (channelIndex < deviceSet->getNumberOfRxChannels()) { + MainCore::MsgDeleteChannel *msg = MainCore::MsgDeleteChannel::create(deviceSetIndex, channelIndex, false); + m_mainCore.m_inputMessageQueue.push(msg); + + response.init(); + *response.getMessage() = QString("Message to delete a channel (MsgDeleteChannel) was submitted successfully"); + + return 202; + } + else + { + error.init(); + *error.getMessage() = QString("There is no channel at index %1. There are %2 Rx channels") + .arg(channelIndex) + .arg(channelIndex < deviceSet->getNumberOfRxChannels()); + return 400; + } + } + else if (deviceSet->m_deviceSinkEngine) // Tx + { + if (channelIndex < deviceSet->getNumberOfTxChannels()) { + MainCore::MsgDeleteChannel *msg = MainCore::MsgDeleteChannel::create(deviceSetIndex, channelIndex, true); + m_mainCore.m_inputMessageQueue.push(msg); + + response.init(); + *response.getMessage() = QString("Message to delete a channel (MsgDeleteChannel) was submitted successfully"); + + return 202; + } + else + { + error.init(); + *error.getMessage() = QString("There is no channel at index %1. There are %2 Tx channels") + .arg(channelIndex) + .arg(channelIndex < deviceSet->getNumberOfRxChannels()); + return 400; + } + } + else + { + error.init(); + *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 WebAPIAdapterSrv::getDeviceSetList(SWGSDRangel::SWGDeviceSetList* deviceSetList) { deviceSetList->init(); diff --git a/sdrsrv/webapi/webapiadaptersrv.h b/sdrsrv/webapi/webapiadaptersrv.h index e362031b5..b8291bb83 100644 --- a/sdrsrv/webapi/webapiadaptersrv.h +++ b/sdrsrv/webapi/webapiadaptersrv.h @@ -152,6 +152,12 @@ public: SWGSDRangel::SWGSuccessResponse& response, SWGSDRangel::SWGErrorResponse& error); + virtual int devicesetChannelDelete( + int deviceSetIndex, + int channelIndex, + SWGSDRangel::SWGSuccessResponse& response, + SWGSDRangel::SWGErrorResponse& error); + private: MainCore& m_mainCore;