diff --git a/sdrbase/plugin/pluginmanager.cpp b/sdrbase/plugin/pluginmanager.cpp index dd719eab4..37eff559b 100644 --- a/sdrbase/plugin/pluginmanager.cpp +++ b/sdrbase/plugin/pluginmanager.cpp @@ -199,3 +199,21 @@ void PluginManager::createTxChannelInstance(int channelPluginIndex, DeviceUISet pluginInterface->createTxChannelGUI(m_txChannelRegistrations[channelPluginIndex].m_channelIdURI, deviceUISet, txChannel); } } + +void PluginManager::createRxChannelServerInstance(int channelPluginIndex, DeviceSourceAPI *deviceAPI) +{ + if (channelPluginIndex < m_rxChannelRegistrations.size()) + { + PluginInterface *pluginInterface = m_rxChannelRegistrations[channelPluginIndex].m_plugin; + pluginInterface->createRxChannel(m_rxChannelRegistrations[channelPluginIndex].m_channelIdURI, deviceAPI); + } +} + +void PluginManager::createTxChannelServerInstance(int channelPluginIndex, DeviceSinkAPI *deviceAPI) +{ + if (channelPluginIndex < m_txChannelRegistrations.size()) + { + PluginInterface *pluginInterface = m_txChannelRegistrations[channelPluginIndex].m_plugin; + pluginInterface->createTxChannel(m_txChannelRegistrations[channelPluginIndex].m_channelIdURI, deviceAPI); + } +} diff --git a/sdrbase/plugin/pluginmanager.h b/sdrbase/plugin/pluginmanager.h index 442a21719..0b587a427 100644 --- a/sdrbase/plugin/pluginmanager.h +++ b/sdrbase/plugin/pluginmanager.h @@ -57,9 +57,11 @@ public: PluginAPI::ChannelRegistrations *getTxChannelRegistrations() { return &m_txChannelRegistrations; } void createRxChannelInstance(int channelPluginIndex, DeviceUISet *deviceUISet, DeviceSourceAPI *deviceAPI); + void createRxChannelServerInstance(int channelPluginIndex, DeviceSourceAPI *deviceAPI); void listRxChannels(QList& list); void createTxChannelInstance(int channelPluginIndex, DeviceUISet *deviceUISet, DeviceSinkAPI *deviceAPI); + void createTxChannelServerInstance(int channelPluginIndex, DeviceSinkAPI *deviceAPI); void listTxChannels(QList& list); static const QString& getFileSourceDeviceId() { return m_fileSourceDeviceTypeID; } diff --git a/sdrsrv/maincore.cpp b/sdrsrv/maincore.cpp index 271bb18a5..86a890b6a 100644 --- a/sdrsrv/maincore.cpp +++ b/sdrsrv/maincore.cpp @@ -41,6 +41,7 @@ MESSAGE_CLASS_DEFINITION(MainCore::MsgDeletePreset, Message) MESSAGE_CLASS_DEFINITION(MainCore::MsgAddDeviceSet, Message) MESSAGE_CLASS_DEFINITION(MainCore::MsgRemoveLastDeviceSet, Message) MESSAGE_CLASS_DEFINITION(MainCore::MsgSetDevice, Message) +MESSAGE_CLASS_DEFINITION(MainCore::MsgAddChannel, Message) MainCore *MainCore::m_instance = 0; @@ -154,6 +155,13 @@ bool MainCore::handleMessage(const Message& cmd) return true; } + else if (MsgAddChannel::match(cmd)) + { + MsgAddChannel& notif = (MsgAddChannel&) cmd; + addChannel(notif.getDeviceSetIndex(), notif.getChannelRegistrationIndex()); + return true; + } + else { return false; @@ -460,6 +468,23 @@ void MainCore::changeSampleSink(int deviceSetIndex, int selectedDeviceIndex) } } +void MainCore::addChannel(int deviceSetIndex, int selectedChannelIndex) +{ + if (deviceSetIndex >= 0) + { + DeviceSet *deviceSet = m_deviceSets[deviceSetIndex]; + + if (deviceSet->m_deviceSourceEngine) // source device => Rx channels + { + m_pluginManager->createRxChannelServerInstance(selectedChannelIndex, deviceSet->m_deviceSourceAPI); + } + else if (deviceSet->m_deviceSinkEngine) // sink device => Tx channels + { + m_pluginManager->createTxChannelServerInstance(selectedChannelIndex, deviceSet->m_deviceSinkAPI); + } + } +} + 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 83434a327..68ce77efa 100644 --- a/sdrsrv/maincore.h +++ b/sdrsrv/maincore.h @@ -65,6 +65,7 @@ public: void removeLastDevice(); void changeSampleSource(int deviceSetIndex, int selectedDeviceIndex); void changeSampleSink(int deviceSetIndex, int selectedDeviceIndex); + void addChannel(int deviceSetIndex, int selectedChannelIndex); friend class WebAPIAdapterSrv; @@ -217,6 +218,32 @@ private: { } }; + class MsgAddChannel : public Message { + MESSAGE_CLASS_DECLARATION + + public: + int getDeviceSetIndex() const { return m_deviceSetIndex; } + int getChannelRegistrationIndex() const { return m_channelRegistrationIndex; } + bool isTx() const { return m_tx; } + + static MsgAddChannel* create(int deviceSetIndex, int channelRegistrationIndex, bool tx) + { + return new MsgAddChannel(deviceSetIndex, channelRegistrationIndex, tx); + } + + private: + int m_deviceSetIndex; + int m_channelRegistrationIndex; + bool m_tx; + + MsgAddChannel(int deviceSetIndex, int channelRegistrationIndex, bool tx) : + Message(), + m_deviceSetIndex(deviceSetIndex), + m_channelRegistrationIndex(channelRegistrationIndex), + 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 ea0f5fccb..44e7622a8 100644 --- a/sdrsrv/webapi/webapiadaptersrv.cpp +++ b/sdrsrv/webapi/webapiadaptersrv.cpp @@ -35,6 +35,7 @@ #include "SWGPresets.h" #include "SWGPresetTransfer.h" #include "SWGDeviceSettings.h" +#include "SWGChannelSettings.h" #include "SWGSuccessResponse.h" #include "SWGErrorResponse.h" @@ -923,6 +924,97 @@ int WebAPIAdapterSrv::devicesetDeviceSettingsPutPatch( } } +int WebAPIAdapterSrv::devicesetChannelPost( + int deviceSetIndex, + SWGSDRangel::SWGChannelSettings& query, + SWGSDRangel::SWGSuccessResponse& response, + SWGSDRangel::SWGErrorResponse& error) +{ + if ((deviceSetIndex >= 0) && (deviceSetIndex < (int) m_mainCore.m_deviceSets.size())) + { + DeviceSet *deviceSet = m_mainCore.m_deviceSets[deviceSetIndex]; + + if (query.getTx() == 0) // Rx + { + if (deviceSet->m_deviceSourceEngine == 0) + { + error.init(); + *error.getMessage() = QString("Device set at %1 is not a receive device set").arg(deviceSetIndex); + return 400; + } + + PluginAPI::ChannelRegistrations *channelRegistrations = m_mainCore.m_pluginManager->getRxChannelRegistrations(); + int nbRegistrations = channelRegistrations->size(); + int index = 0; + for (; index < nbRegistrations; index++) + { + if (channelRegistrations->at(index).m_channelId == *query.getChannelType()) { + break; + } + } + + if (index < nbRegistrations) + { + MainCore::MsgAddChannel *msg = MainCore::MsgAddChannel::create(deviceSetIndex, index, false); + m_mainCore.m_inputMessageQueue.push(msg); + + response.init(); + *response.getMessage() = QString("Message to add a channel (MsgAddChannel) was submitted successfully"); + + return 202; + } + else + { + error.init(); + *error.getMessage() = QString("There is no receive channel with id %1").arg(*query.getChannelType()); + return 404; + } + } + else // Tx + { + if (deviceSet->m_deviceSinkEngine == 0) + { + error.init(); + *error.getMessage() = QString("Device set at %1 is not a transmit device set").arg(deviceSetIndex); + return 400; + } + + PluginAPI::ChannelRegistrations *channelRegistrations = m_mainCore.m_pluginManager->getTxChannelRegistrations(); + int nbRegistrations = channelRegistrations->size(); + int index = 0; + for (; index < nbRegistrations; index++) + { + if (channelRegistrations->at(index).m_channelId == *query.getChannelType()) { + break; + } + } + + if (index < nbRegistrations) + { + MainCore::MsgAddChannel *msg = MainCore::MsgAddChannel::create(deviceSetIndex, index, true); + m_mainCore.m_inputMessageQueue.push(msg); + + response.init(); + *response.getMessage() = QString("Message to add a channel (MsgAddChannel) was submitted successfully"); + + return 202; + } + else + { + error.init(); + *error.getMessage() = QString("There is no transmit channel with id %1").arg(*query.getChannelType()); + return 404; + } + } + } + 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 97257c082..e362031b5 100644 --- a/sdrsrv/webapi/webapiadaptersrv.h +++ b/sdrsrv/webapi/webapiadaptersrv.h @@ -146,6 +146,12 @@ public: SWGSDRangel::SWGDeviceSettings& response, SWGSDRangel::SWGErrorResponse& error); + virtual int devicesetChannelPost( + int deviceSetIndex, + SWGSDRangel::SWGChannelSettings& query, + SWGSDRangel::SWGSuccessResponse& response, + SWGSDRangel::SWGErrorResponse& error); + private: MainCore& m_mainCore;