1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-18 14:21:49 -05:00
sdrangel/sdrbase/device/deviceset.cpp

416 lines
18 KiB
C++
Raw Normal View History

2017-12-17 17:15:42 -05:00
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2017 Edouard Griffiths, F4EXB //
// //
// 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 //
// (at your option) any later version. //
2017-12-17 17:15:42 -05:00
// //
// 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 <http://www.gnu.org/licenses/>. //
///////////////////////////////////////////////////////////////////////////////////
#include <algorithm>
2017-12-17 17:15:42 -05:00
#include "dsp/dspdevicesourceengine.h"
#include "dsp/dspdevicesinkengine.h"
#include "plugin/pluginapi.h"
#include "plugin/plugininterface.h"
#include "settings/preset.h"
2019-05-09 11:27:12 -04:00
#include "channel/channelapi.h"
#include "channel/channelutils.h"
2017-12-17 17:15:42 -05:00
#include "settings/preset.h"
#include "deviceset.h"
2019-05-09 11:27:12 -04:00
DeviceSet::ChannelInstanceRegistration::ChannelInstanceRegistration(const QString& channelName, ChannelAPI* channelAPI) :
m_channelName(channelName),
m_channelAPI(channelAPI)
{}
2019-05-09 11:27:12 -04:00
2017-12-17 17:15:42 -05:00
DeviceSet::DeviceSet(int tabIndex)
{
m_deviceAPI = nullptr;
m_deviceSourceEngine = nullptr;
m_deviceSinkEngine = nullptr;
m_deviceMIMOEngine = nullptr;
2017-12-17 17:15:42 -05:00
m_deviceTabIndex = tabIndex;
}
DeviceSet::~DeviceSet()
{
}
void DeviceSet::freeChannels()
{
for(int i = 0; i < m_channelInstanceRegistrations.count(); i++)
{
qDebug("DeviceSet::freeChannels: destroying channel [%s]", qPrintable(m_channelInstanceRegistrations[i].m_channelName));
m_channelInstanceRegistrations[i].m_channelAPI->destroy();
}
}
void DeviceSet::deleteChannel(int channelIndex)
{
if (channelIndex < m_channelInstanceRegistrations.count())
{
m_channelInstanceRegistrations[channelIndex].m_channelAPI->destroy();
m_channelInstanceRegistrations.removeAt(channelIndex);
renameChannelInstances();
}
}
void DeviceSet::addRxChannel(int selectedChannelIndex, PluginAPI *pluginAPI)
{
PluginAPI::ChannelRegistrations *channelRegistrations = pluginAPI->getRxChannelRegistrations(); // Available channel plugins
ChannelAPI *rxChannel;
(*channelRegistrations)[selectedChannelIndex].m_plugin->createRxChannel(m_deviceAPI, nullptr, &rxChannel);
ChannelInstanceRegistration reg = ChannelInstanceRegistration(rxChannel->getName(), rxChannel);
m_channelInstanceRegistrations.append(reg);
qDebug("DeviceSet::addRxChannel: %s", qPrintable(rxChannel->getName()));
}
void DeviceSet::addTxChannel(int selectedChannelIndex, PluginAPI *pluginAPI)
{
PluginAPI::ChannelRegistrations *channelRegistrations = pluginAPI->getTxChannelRegistrations(); // Available channel plugins
ChannelAPI *txChannel;
(*channelRegistrations)[selectedChannelIndex].m_plugin->createTxChannel(m_deviceAPI, nullptr, &txChannel);
ChannelInstanceRegistration reg = ChannelInstanceRegistration(txChannel->getName(), txChannel);
m_channelInstanceRegistrations.append(reg);
qDebug("DeviceSet::addTxChannel: %s", qPrintable(txChannel->getName()));
}
void DeviceSet::addMIMOChannel(int selectedChannelIndex, PluginAPI *pluginAPI)
{
PluginAPI::ChannelRegistrations *channelRegistrations = pluginAPI->getMIMOChannelRegistrations(); // Available channel plugins
ChannelAPI *mimoChannel;
(*channelRegistrations)[selectedChannelIndex].m_plugin->createMIMOChannel(m_deviceAPI, nullptr, &mimoChannel);
ChannelInstanceRegistration reg = ChannelInstanceRegistration(mimoChannel->getName(), mimoChannel);
m_channelInstanceRegistrations.append(reg);
qDebug("DeviceSet::addMIMOChannel: %s", qPrintable(mimoChannel->getName()));
}
2017-12-17 17:15:42 -05:00
void DeviceSet::loadRxChannelSettings(const Preset *preset, PluginAPI *pluginAPI)
{
if (preset->isSourcePreset())
{
qDebug("DeviceSet::loadChannelSettings: Loading preset [%s | %s]", qPrintable(preset->getGroup()), qPrintable(preset->getDescription()));
// Available channel plugins
PluginAPI::ChannelRegistrations *channelRegistrations = pluginAPI->getRxChannelRegistrations();
// copy currently open channels and clear list
ChannelInstanceRegistrations openChannels = m_channelInstanceRegistrations;
m_channelInstanceRegistrations.clear();
2017-12-17 17:15:42 -05:00
qDebug("DeviceSet::loadChannelSettings: %d channel(s) in preset", preset->getChannelCount());
for (int i = 0; i < preset->getChannelCount(); i++)
2017-12-17 17:15:42 -05:00
{
const Preset::ChannelConfig& channelConfig = preset->getChannelConfig(i);
ChannelInstanceRegistration reg;
// if we have one instance available already, use it
for (int i = 0; i < openChannels.count(); i++)
2017-12-17 17:15:42 -05:00
{
qDebug("DeviceSet::loadChannelSettings: channels compare [%s] vs [%s]", qPrintable(openChannels[i].m_channelName), qPrintable(channelConfig.m_channelIdURI));
//if(openChannels[i].m_channelName == channelConfig.m_channelIdURI)
if (ChannelUtils::compareChannelURIs(openChannels[i].m_channelName, channelConfig.m_channelIdURI))
2017-12-17 17:15:42 -05:00
{
qDebug("DeviceSet::loadChannelSettings: channel [%s] found", qPrintable(openChannels[i].m_channelName));
reg = openChannels.takeAt(i);
m_channelInstanceRegistrations.append(reg);
2017-12-17 17:15:42 -05:00
break;
}
}
// if we haven't one already, create one
if (reg.m_channelAPI == nullptr)
2017-12-17 17:15:42 -05:00
{
for (int i = 0; i < channelRegistrations->count(); i++)
2017-12-17 17:15:42 -05:00
{
//if((*channelRegistrations)[i].m_channelIdURI == channelConfig.m_channelIdURI)
if (ChannelUtils::compareChannelURIs((*channelRegistrations)[i].m_channelIdURI, channelConfig.m_channelIdURI))
2017-12-17 17:15:42 -05:00
{
qDebug("DeviceSet::loadChannelSettings: creating new channel [%s] from config [%s]",
qPrintable((*channelRegistrations)[i].m_channelIdURI),
qPrintable(channelConfig.m_channelIdURI));
ChannelAPI *rxChannel;
(*channelRegistrations)[i].m_plugin->createRxChannel(m_deviceAPI, nullptr, &rxChannel);
reg = ChannelInstanceRegistration(channelConfig.m_channelIdURI, rxChannel);
m_channelInstanceRegistrations.append(reg);
2017-12-17 17:15:42 -05:00
break;
}
}
}
if (reg.m_channelAPI != nullptr)
2017-12-17 17:15:42 -05:00
{
qDebug("DeviceSet::loadChannelSettings: deserializing channel [%s]", qPrintable(channelConfig.m_channelIdURI));
reg.m_channelAPI->deserialize(channelConfig.m_config);
2017-12-17 17:15:42 -05:00
}
}
// everything, that is still "available" is not needed anymore
for (int i = 0; i < openChannels.count(); i++)
2017-12-17 17:15:42 -05:00
{
qDebug("DeviceSet::loadChannelSettings: destroying spare channel [%s]", qPrintable(openChannels[i].m_channelName));
openChannels[i].m_channelAPI->destroy();
2017-12-17 17:15:42 -05:00
}
renameChannelInstances();
2017-12-17 17:15:42 -05:00
}
else
{
qDebug("DeviceSet::loadChannelSettings: Loading preset [%s | %s] not a source preset", qPrintable(preset->getGroup()), qPrintable(preset->getDescription()));
}
}
void DeviceSet::saveRxChannelSettings(Preset *preset)
{
if (preset->isSourcePreset())
{
std::sort(m_channelInstanceRegistrations.begin(), m_channelInstanceRegistrations.end()); // sort by increasing delta frequency and type
2017-12-17 17:15:42 -05:00
for (int i = 0; i < m_channelInstanceRegistrations.count(); i++)
2017-12-17 17:15:42 -05:00
{
qDebug("DeviceSet::saveChannelSettings: channel [%s] saved", qPrintable(m_channelInstanceRegistrations[i].m_channelName));
preset->addChannel(m_channelInstanceRegistrations[i].m_channelName, m_channelInstanceRegistrations[i].m_channelAPI->serialize());
2017-12-17 17:15:42 -05:00
}
}
else
{
qDebug("DeviceSet::saveChannelSettings: not a source preset");
}
}
void DeviceSet::loadTxChannelSettings(const Preset *preset, PluginAPI *pluginAPI)
{
2019-09-13 07:40:31 -04:00
if (preset->isSinkPreset())
2017-12-17 17:15:42 -05:00
{
2019-09-13 07:40:31 -04:00
qDebug("DeviceSet::loadTxChannelSettings: Loading preset [%s | %s]", qPrintable(preset->getGroup()), qPrintable(preset->getDescription()));
2017-12-17 17:15:42 -05:00
// Available channel plugins
PluginAPI::ChannelRegistrations *channelRegistrations = pluginAPI->getTxChannelRegistrations();
// copy currently open channels and clear list
ChannelInstanceRegistrations openChannels = m_channelInstanceRegistrations;
m_channelInstanceRegistrations.clear();
2017-12-17 17:15:42 -05:00
2019-09-13 07:40:31 -04:00
qDebug("DeviceSet::loadTxChannelSettings: %d channel(s) in preset", preset->getChannelCount());
2017-12-17 17:15:42 -05:00
for (int i = 0; i < preset->getChannelCount(); i++)
2017-12-17 17:15:42 -05:00
{
const Preset::ChannelConfig& channelConfig = preset->getChannelConfig(i);
ChannelInstanceRegistration reg;
// if we have one instance available already, use it
for (int i = 0; i < openChannels.count(); i++)
2017-12-17 17:15:42 -05:00
{
2019-09-13 07:40:31 -04:00
qDebug("DeviceSet::loadTxChannelSettings: channels compare [%s] vs [%s]", qPrintable(openChannels[i].m_channelName), qPrintable(channelConfig.m_channelIdURI));
2017-12-17 17:15:42 -05:00
if (openChannels[i].m_channelName == channelConfig.m_channelIdURI)
2017-12-17 17:15:42 -05:00
{
2019-09-13 07:40:31 -04:00
qDebug("DeviceSet::loadTxChannelSettings: channel [%s] found", qPrintable(openChannels[i].m_channelName));
2017-12-17 17:15:42 -05:00
reg = openChannels.takeAt(i);
m_channelInstanceRegistrations.append(reg);
2017-12-17 17:15:42 -05:00
break;
}
}
// if we haven't one already, create one
if (reg.m_channelAPI == nullptr)
2017-12-17 17:15:42 -05:00
{
for (int i = 0; i < channelRegistrations->count(); i++)
2017-12-17 17:15:42 -05:00
{
if ((*channelRegistrations)[i].m_channelIdURI == channelConfig.m_channelIdURI)
2017-12-17 17:15:42 -05:00
{
2019-09-13 07:40:31 -04:00
qDebug("DeviceSet::loadTxChannelSettings: creating new channel [%s]", qPrintable(channelConfig.m_channelIdURI));
ChannelAPI *txChannel;
(*channelRegistrations)[i].m_plugin->createTxChannel(m_deviceAPI, nullptr, &txChannel);
reg = ChannelInstanceRegistration(channelConfig.m_channelIdURI, txChannel);
m_channelInstanceRegistrations.append(reg);
2017-12-17 17:15:42 -05:00
break;
}
}
}
if (reg.m_channelAPI != nullptr)
2017-12-17 17:15:42 -05:00
{
2019-09-13 07:40:31 -04:00
qDebug("DeviceSet::loadTxChannelSettings: deserializing channel [%s]", qPrintable(channelConfig.m_channelIdURI));
reg.m_channelAPI->deserialize(channelConfig.m_config);
2017-12-17 17:15:42 -05:00
}
}
// everything, that is still "available" is not needed anymore
for (int i = 0; i < openChannels.count(); i++)
2017-12-17 17:15:42 -05:00
{
2019-09-13 07:40:31 -04:00
qDebug("DeviceSet::loadTxChannelSettings: destroying spare channel [%s]", qPrintable(openChannels[i].m_channelName));
openChannels[i].m_channelAPI->destroy();
2017-12-17 17:15:42 -05:00
}
renameChannelInstances();
2017-12-17 17:15:42 -05:00
}
2019-09-13 07:40:31 -04:00
else
{
qDebug("DeviceSet::loadTxChannelSettings: Loading preset [%s | %s] not a sink preset", qPrintable(preset->getGroup()), qPrintable(preset->getDescription()));
}
2017-12-17 17:15:42 -05:00
}
void DeviceSet::saveTxChannelSettings(Preset *preset)
{
2019-09-13 07:40:31 -04:00
if (preset->isSinkPreset())
2017-12-17 17:15:42 -05:00
{
std::sort(m_channelInstanceRegistrations.begin(), m_channelInstanceRegistrations.end()); // sort by increasing delta frequency and type
2019-09-13 07:40:31 -04:00
for (int i = 0; i < m_channelInstanceRegistrations.count(); i++)
{
qDebug("DeviceSet::saveTxChannelSettings: channel [%s] saved", qPrintable(m_channelInstanceRegistrations[i].m_channelName));
preset->addChannel(m_channelInstanceRegistrations[i].m_channelName, m_channelInstanceRegistrations[i].m_channelAPI->serialize());
}
2017-12-17 17:15:42 -05:00
}
else
2019-09-13 07:40:31 -04:00
{
qDebug("DeviceSet::saveTxChannelSettings: not a sink preset");
}
}
void DeviceSet::loadMIMOChannelSettings(const Preset *preset, PluginAPI *pluginAPI)
{
if (preset->isMIMOPreset())
{
qDebug("DeviceSet::loadMIMOChannelSettings: Loading preset [%s | %s]", qPrintable(preset->getGroup()), qPrintable(preset->getDescription()));
// Available channel plugins
PluginAPI::ChannelRegistrations *channelRegistrations = pluginAPI->getMIMOChannelRegistrations();
// copy currently open channels and clear list
ChannelInstanceRegistrations openChannels = m_channelInstanceRegistrations;
m_channelInstanceRegistrations.clear();
qDebug("DeviceSet::loadMIMOChannelSettings: %d channel(s) in preset", preset->getChannelCount());
for (int i = 0; i < preset->getChannelCount(); i++)
{
const Preset::ChannelConfig& channelConfig = preset->getChannelConfig(i);
ChannelInstanceRegistration reg;
// if we have one instance available already, use it
for (int i = 0; i < openChannels.count(); i++)
{
qDebug("DeviceSet::loadMIMOChannelSettings: channels compare [%s] vs [%s]", qPrintable(openChannels[i].m_channelName), qPrintable(channelConfig.m_channelIdURI));
//if(openChannels[i].m_channelName == channelConfig.m_channelIdURI)
if (ChannelUtils::compareChannelURIs(openChannels[i].m_channelName, channelConfig.m_channelIdURI))
{
qDebug("DeviceSet::loadMIMOChannelSettings: channel [%s] found", qPrintable(openChannels[i].m_channelName));
reg = openChannels.takeAt(i);
m_channelInstanceRegistrations.append(reg);
break;
}
}
// if we haven't one already, create one
if (reg.m_channelAPI == nullptr)
{
for (int i = 0; i < channelRegistrations->count(); i++)
{
//if((*channelRegistrations)[i].m_channelIdURI == channelConfig.m_channelIdURI)
if (ChannelUtils::compareChannelURIs((*channelRegistrations)[i].m_channelIdURI, channelConfig.m_channelIdURI))
{
qDebug("DeviceSet::loadMIMOChannelSettings: creating new channel [%s] from config [%s]",
qPrintable((*channelRegistrations)[i].m_channelIdURI),
qPrintable(channelConfig.m_channelIdURI));
ChannelAPI *mimoChannel;
(*channelRegistrations)[i].m_plugin->createMIMOChannel(m_deviceAPI, nullptr, &mimoChannel);
2019-09-13 07:40:31 -04:00
reg = ChannelInstanceRegistration(channelConfig.m_channelIdURI, mimoChannel);
m_channelInstanceRegistrations.append(reg);
break;
}
}
}
if (reg.m_channelAPI != nullptr)
{
qDebug("DeviceSet::loadMIMOChannelSettings: deserializing channel [%s]", qPrintable(channelConfig.m_channelIdURI));
reg.m_channelAPI->deserialize(channelConfig.m_config);
}
}
// everything, that is still "available" is not needed anymore
for (int i = 0; i < openChannels.count(); i++)
{
qDebug("DeviceSet::loadMIMOChannelSettings: destroying spare channel [%s]", qPrintable(openChannels[i].m_channelName));
openChannels[i].m_channelAPI->destroy();
}
renameChannelInstances();
}
else
{
qDebug("DeviceSet::loadChannelSettings: Loading preset [%s | %s] not a MIMO preset", qPrintable(preset->getGroup()), qPrintable(preset->getDescription()));
}
}
void DeviceSet::saveMIMOChannelSettings(Preset *preset)
{
if (preset->isMIMOPreset())
2017-12-17 17:15:42 -05:00
{
std::sort(m_channelInstanceRegistrations.begin(), m_channelInstanceRegistrations.end()); // sort by increasing delta frequency and type
2017-12-17 17:15:42 -05:00
for (int i = 0; i < m_channelInstanceRegistrations.count(); i++)
2017-12-17 17:15:42 -05:00
{
2019-09-13 07:40:31 -04:00
qDebug("DeviceSet::saveMIMOChannelSettings: channel [%s] saved", qPrintable(m_channelInstanceRegistrations[i].m_channelName));
preset->addChannel(m_channelInstanceRegistrations[i].m_channelName, m_channelInstanceRegistrations[i].m_channelAPI->serialize());
2017-12-17 17:15:42 -05:00
}
}
2019-09-13 07:40:31 -04:00
else
{
qDebug("DeviceSet::saveMIMOChannelSettings: not a MIMO preset");
}
2017-12-17 17:15:42 -05:00
}
void DeviceSet::renameChannelInstances()
2017-12-17 17:15:42 -05:00
{
for (int i = 0; i < m_channelInstanceRegistrations.count(); i++)
2017-12-17 17:15:42 -05:00
{
m_channelInstanceRegistrations[i].m_channelAPI->setName(QString("%1:%2").arg(m_channelInstanceRegistrations[i].m_channelName).arg(i));
2017-12-17 17:15:42 -05:00
}
}
// sort by increasing delta frequency and type (i.e. name)
bool DeviceSet::ChannelInstanceRegistration::operator<(const ChannelInstanceRegistration& other) const
{
if (m_channelAPI && other.m_channelAPI)
2017-12-17 17:15:42 -05:00
{
if (m_channelAPI->getCenterFrequency() == other.m_channelAPI->getCenterFrequency())
2017-12-17 17:15:42 -05:00
{
return m_channelAPI->getName() < other.m_channelAPI->getName();
2017-12-17 17:15:42 -05:00
}
else
{
return m_channelAPI->getCenterFrequency() < other.m_channelAPI->getCenterFrequency();
2017-12-17 17:15:42 -05:00
}
}
else
{
return false;
}
}