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 //
2019-04-11 08:44:23 -04:00
// (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 "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"
2019-08-01 19:58:59 -04:00
# 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 ) ,
2019-09-07 18:44:56 -04:00
m_channelAPI ( channelAPI )
{ }
2019-05-09 11:27:12 -04:00
2017-12-17 17:15:42 -05:00
DeviceSet : : DeviceSet ( int tabIndex )
{
2019-05-20 10:31:15 -04:00
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 ( )
{
}
2019-05-09 11:27:12 -04:00
void DeviceSet : : registerRxChannelInstance ( const QString & channelName , ChannelAPI * channelAPI )
2017-12-17 17:15:42 -05:00
{
2019-09-07 18:44:56 -04:00
m_channelInstanceRegistrations . append ( ChannelInstanceRegistration ( channelName , channelAPI ) ) ;
renameChannelInstances ( ) ;
2017-12-17 17:15:42 -05:00
}
2019-05-09 11:27:12 -04:00
void DeviceSet : : registerTxChannelInstance ( const QString & channelName , ChannelAPI * channelAPI )
2017-12-17 17:15:42 -05:00
{
2019-09-07 18:44:56 -04:00
m_channelInstanceRegistrations . append ( ChannelInstanceRegistration ( channelName , channelAPI ) ) ;
renameChannelInstances ( ) ;
}
void DeviceSet : : registerChannelInstance ( const QString & channelName , ChannelAPI * channelAPI )
{
m_channelInstanceRegistrations . append ( ChannelInstanceRegistration ( channelName , channelAPI ) ) ;
renameChannelInstances ( ) ;
2017-12-17 17:15:42 -05:00
}
2019-05-09 11:27:12 -04:00
void DeviceSet : : removeRxChannelInstance ( ChannelAPI * channelAPI )
2017-12-17 17:15:42 -05:00
{
2019-09-07 18:44:56 -04:00
for ( ChannelInstanceRegistrations : : iterator it = m_channelInstanceRegistrations . begin ( ) ; it ! = m_channelInstanceRegistrations . end ( ) ; + + it )
2017-12-17 17:15:42 -05:00
{
2019-09-07 18:44:56 -04:00
if ( it - > m_channelAPI = = channelAPI )
2017-12-17 17:15:42 -05:00
{
2019-09-07 18:44:56 -04:00
m_channelInstanceRegistrations . erase ( it ) ;
2017-12-17 17:15:42 -05:00
break ;
}
}
2019-09-07 18:44:56 -04:00
renameChannelInstances ( ) ;
2017-12-17 17:15:42 -05:00
}
2019-05-09 11:27:12 -04:00
void DeviceSet : : removeTxChannelInstance ( ChannelAPI * channelAPI )
2017-12-17 17:15:42 -05:00
{
2019-09-07 18:44:56 -04:00
for ( ChannelInstanceRegistrations : : iterator it = m_channelInstanceRegistrations . begin ( ) ; it ! = m_channelInstanceRegistrations . end ( ) ; + + it )
2017-12-17 17:15:42 -05:00
{
2019-09-07 18:44:56 -04:00
if ( it - > m_channelAPI = = channelAPI )
2017-12-17 17:15:42 -05:00
{
2019-09-07 18:44:56 -04:00
m_channelInstanceRegistrations . erase ( it ) ;
2017-12-17 17:15:42 -05:00
break ;
}
}
2019-09-07 18:44:56 -04:00
renameChannelInstances ( ) ;
2017-12-17 17:15:42 -05:00
}
2019-09-07 18:44:56 -04:00
void DeviceSet : : removeChannelInstance ( ChannelAPI * channelAPI )
2017-12-17 17:15:42 -05:00
{
2019-09-07 18:44:56 -04:00
for ( ChannelInstanceRegistrations : : iterator it = m_channelInstanceRegistrations . begin ( ) ; it ! = m_channelInstanceRegistrations . end ( ) ; + + it )
2017-12-17 17:15:42 -05:00
{
2019-09-07 18:44:56 -04:00
if ( it - > m_channelAPI = = channelAPI )
{
m_channelInstanceRegistrations . erase ( it ) ;
break ;
}
2017-12-17 17:15:42 -05:00
}
2019-09-07 18:44:56 -04:00
renameChannelInstances ( ) ;
2017-12-17 17:15:42 -05:00
}
2019-09-07 18:44:56 -04:00
void DeviceSet : : freeChannels ( )
2017-12-23 16:33:30 -05:00
{
2019-09-07 18:44:56 -04:00
for ( int i = 0 ; i < m_channelInstanceRegistrations . count ( ) ; i + + )
2017-12-23 16:33:30 -05:00
{
2019-09-07 18:44:56 -04:00
qDebug ( " DeviceSet::freeChannels: destroying channel [%s] " , qPrintable ( m_channelInstanceRegistrations [ i ] . m_channelName ) ) ;
m_channelInstanceRegistrations [ i ] . m_channelAPI - > destroy ( ) ;
2017-12-23 16:33:30 -05:00
}
}
2019-09-07 18:44:56 -04:00
void DeviceSet : : deleteChannel ( int channelIndex )
2017-12-23 16:33:30 -05:00
{
2019-09-07 18:44:56 -04:00
if ( channelIndex < m_channelInstanceRegistrations . count ( ) )
2017-12-23 16:33:30 -05:00
{
2019-09-07 18:44:56 -04:00
m_channelInstanceRegistrations [ channelIndex ] . m_channelAPI - > destroy ( ) ;
m_channelInstanceRegistrations . removeAt ( channelIndex ) ;
renameChannelInstances ( ) ;
2017-12-23 16:33:30 -05:00
}
}
2017-12-22 23:56:40 -05:00
void DeviceSet : : addRxChannel ( int selectedChannelIndex , PluginAPI * pluginAPI )
{
PluginAPI : : ChannelRegistrations * channelRegistrations = pluginAPI - > getRxChannelRegistrations ( ) ; // Available channel plugins
2019-05-09 11:27:12 -04:00
ChannelAPI * rxChannel = ( * channelRegistrations ) [ selectedChannelIndex ] . m_plugin - > createRxChannelCS ( m_deviceAPI ) ;
2017-12-22 23:56:40 -05:00
ChannelInstanceRegistration reg = ChannelInstanceRegistration ( rxChannel - > getName ( ) , rxChannel ) ;
2019-09-07 18:44:56 -04:00
m_channelInstanceRegistrations . append ( reg ) ;
2017-12-22 23:56:40 -05:00
qDebug ( " DeviceSet::addRxChannel: %s " , qPrintable ( rxChannel - > getName ( ) ) ) ;
}
void DeviceSet : : addTxChannel ( int selectedChannelIndex , PluginAPI * pluginAPI )
{
PluginAPI : : ChannelRegistrations * channelRegistrations = pluginAPI - > getTxChannelRegistrations ( ) ; // Available channel plugins
2019-05-09 11:27:12 -04:00
ChannelAPI * txChannel = ( * channelRegistrations ) [ selectedChannelIndex ] . m_plugin - > createTxChannelCS ( m_deviceAPI ) ;
2017-12-22 23:56:40 -05:00
ChannelInstanceRegistration reg = ChannelInstanceRegistration ( txChannel - > getName ( ) , txChannel ) ;
2019-09-07 18:44:56 -04:00
m_channelInstanceRegistrations . append ( reg ) ;
2017-12-22 23:56:40 -05:00
qDebug ( " DeviceSet::addTxChannel: %s " , qPrintable ( txChannel - > getName ( ) ) ) ;
}
2019-09-07 18:44:56 -04:00
void DeviceSet : : addMIMOChannel ( int selectedChannelIndex , PluginAPI * pluginAPI )
{
PluginAPI : : ChannelRegistrations * channelRegistrations = pluginAPI - > getMIMOChannelRegistrations ( ) ; // Available channel plugins
ChannelAPI * mimoChannel = ( * channelRegistrations ) [ selectedChannelIndex ] . m_plugin - > createMIMOChannelCS ( m_deviceAPI ) ;
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
2019-09-07 18:44:56 -04:00
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 ( ) ) ;
2018-05-30 05:49:54 -04: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
2018-05-30 05:49:54 -04:00
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 ) ) ;
2018-05-30 05:49:54 -04:00
//if(openChannels[i].m_channelName == channelConfig.m_channelIdURI)
2019-08-04 14:24:44 -04:00
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 ) ;
2019-09-07 18:44:56 -04:00
m_channelInstanceRegistrations . append ( reg ) ;
2017-12-17 17:15:42 -05:00
break ;
}
}
// if we haven't one already, create one
2019-09-07 18:44:56 -04:00
if ( reg . m_channelAPI = = nullptr )
2017-12-17 17:15:42 -05:00
{
2018-05-30 05:49:54 -04:00
for ( int i = 0 ; i < channelRegistrations - > count ( ) ; i + + )
2017-12-17 17:15:42 -05:00
{
2018-05-30 05:49:54 -04:00
//if((*channelRegistrations)[i].m_channelIdURI == channelConfig.m_channelIdURI)
2019-08-04 14:24:44 -04:00
if ( ChannelUtils : : compareChannelURIs ( ( * channelRegistrations ) [ i ] . m_channelIdURI , channelConfig . m_channelIdURI ) )
2017-12-17 17:15:42 -05:00
{
2018-05-30 05:49:54 -04:00
qDebug ( " DeviceSet::loadChannelSettings: creating new channel [%s] from config [%s] " ,
qPrintable ( ( * channelRegistrations ) [ i ] . m_channelIdURI ) ,
qPrintable ( channelConfig . m_channelIdURI ) ) ;
2019-05-09 11:27:12 -04:00
ChannelAPI * rxChannel = ( * channelRegistrations ) [ i ] . m_plugin - > createRxChannelCS ( m_deviceAPI ) ;
2017-12-22 23:56:40 -05:00
reg = ChannelInstanceRegistration ( channelConfig . m_channelIdURI , rxChannel ) ;
2019-09-07 18:44:56 -04:00
m_channelInstanceRegistrations . append ( reg ) ;
2017-12-17 17:15:42 -05:00
break ;
}
}
}
2019-09-07 18:44:56 -04:00
if ( reg . m_channelAPI ! = nullptr )
2017-12-17 17:15:42 -05:00
{
qDebug ( " DeviceSet::loadChannelSettings: deserializing channel [%s] " , qPrintable ( channelConfig . m_channelIdURI ) ) ;
2019-09-07 18:44:56 -04:00
reg . m_channelAPI - > deserialize ( channelConfig . m_config ) ;
2017-12-17 17:15:42 -05:00
}
}
// everything, that is still "available" is not needed anymore
2018-05-30 05:49:54 -04:00
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 ) ) ;
2019-09-07 18:44:56 -04:00
openChannels [ i ] . m_channelAPI - > destroy ( ) ;
2017-12-17 17:15:42 -05:00
}
2019-09-07 18:44:56 -04: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 ( ) )
{
2019-09-07 18:44:56 -04:00
qSort ( m_channelInstanceRegistrations . begin ( ) , m_channelInstanceRegistrations . end ( ) ) ; // sort by increasing delta frequency and type
2017-12-17 17:15:42 -05:00
2019-09-07 18:44:56 -04:00
for ( int i = 0 ; i < m_channelInstanceRegistrations . count ( ) ; i + + )
2017-12-17 17:15:42 -05:00
{
2019-09-07 18:44:56 -04: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 )
{
if ( preset - > isSourcePreset ( ) )
{
qDebug ( " DeviceSet::loadChannelSettings: Loading preset [%s | %s] not a sink preset " , qPrintable ( preset - > getGroup ( ) ) , qPrintable ( preset - > getDescription ( ) ) ) ;
}
else
{
qDebug ( " DeviceSet::loadChannelSettings: Loading preset [%s | %s] " , qPrintable ( preset - > getGroup ( ) ) , qPrintable ( preset - > getDescription ( ) ) ) ;
// Available channel plugins
PluginAPI : : ChannelRegistrations * channelRegistrations = pluginAPI - > getTxChannelRegistrations ( ) ;
// copy currently open channels and clear list
2019-09-07 18:44:56 -04:00
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 ( ) ) ;
2019-09-07 18:44:56 -04: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
2019-09-07 18:44:56 -04:00
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 ) ) ;
2019-09-07 18:44:56 -04:00
if ( 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 ) ;
2019-09-07 18:44:56 -04:00
m_channelInstanceRegistrations . append ( reg ) ;
2017-12-17 17:15:42 -05:00
break ;
}
}
// if we haven't one already, create one
2019-09-07 18:44:56 -04:00
if ( reg . m_channelAPI = = nullptr )
2017-12-17 17:15:42 -05:00
{
2019-09-07 18:44:56 -04:00
for ( int i = 0 ; i < channelRegistrations - > count ( ) ; i + + )
2017-12-17 17:15:42 -05:00
{
2019-09-07 18:44:56 -04:00
if ( ( * channelRegistrations ) [ i ] . m_channelIdURI = = channelConfig . m_channelIdURI )
2017-12-17 17:15:42 -05:00
{
qDebug ( " DeviceSet::loadChannelSettings: creating new channel [%s] " , qPrintable ( channelConfig . m_channelIdURI ) ) ;
2019-05-09 11:27:12 -04:00
ChannelAPI * txChannel = ( * channelRegistrations ) [ i ] . m_plugin - > createTxChannelCS ( m_deviceAPI ) ;
2017-12-22 23:56:40 -05:00
reg = ChannelInstanceRegistration ( channelConfig . m_channelIdURI , txChannel ) ;
2019-09-07 18:44:56 -04:00
m_channelInstanceRegistrations . append ( reg ) ;
2017-12-17 17:15:42 -05:00
break ;
}
}
}
2019-09-07 18:44:56 -04:00
if ( reg . m_channelAPI ! = nullptr )
2017-12-17 17:15:42 -05:00
{
qDebug ( " DeviceSet::loadChannelSettings: deserializing channel [%s] " , qPrintable ( channelConfig . m_channelIdURI ) ) ;
2019-09-07 18:44:56 -04:00
reg . m_channelAPI - > deserialize ( channelConfig . m_config ) ;
2017-12-17 17:15:42 -05:00
}
}
// everything, that is still "available" is not needed anymore
2019-09-07 18:44:56 -04:00
for ( int i = 0 ; i < openChannels . count ( ) ; i + + )
2017-12-17 17:15:42 -05:00
{
2018-02-27 13:26:52 -05:00
qDebug ( " DeviceSet::loadChannelSettings: destroying spare channel [%s] " , qPrintable ( openChannels [ i ] . m_channelName ) ) ;
2019-09-07 18:44:56 -04:00
openChannels [ i ] . m_channelAPI - > destroy ( ) ;
2017-12-17 17:15:42 -05:00
}
2019-09-07 18:44:56 -04:00
renameChannelInstances ( ) ;
2017-12-17 17:15:42 -05:00
}
}
void DeviceSet : : saveTxChannelSettings ( Preset * preset )
{
if ( preset - > isSourcePreset ( ) )
{
qDebug ( " DeviceSet::saveChannelSettings: not a sink preset " ) ;
}
else
{
2019-09-07 18:44:56 -04:00
qSort ( m_channelInstanceRegistrations . begin ( ) , m_channelInstanceRegistrations . end ( ) ) ; // sort by increasing delta frequency and type
2017-12-17 17:15:42 -05:00
2019-09-07 18:44:56 -04:00
for ( int i = 0 ; i < m_channelInstanceRegistrations . count ( ) ; i + + )
2017-12-17 17:15:42 -05:00
{
2019-09-07 18:44:56 -04: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
}
}
}
2019-09-07 18:44:56 -04:00
void DeviceSet : : renameChannelInstances ( )
2017-12-17 17:15:42 -05:00
{
2019-09-07 18:44:56 -04:00
for ( int i = 0 ; i < m_channelInstanceRegistrations . count ( ) ; i + + )
2017-12-17 17:15:42 -05:00
{
2019-09-07 18:44:56 -04: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
{
2019-09-07 18:44:56 -04:00
if ( m_channelAPI & & other . m_channelAPI )
2017-12-17 17:15:42 -05:00
{
2019-09-07 18:44:56 -04:00
if ( m_channelAPI - > getCenterFrequency ( ) = = other . m_channelAPI - > getCenterFrequency ( ) )
2017-12-17 17:15:42 -05:00
{
2019-09-07 18:44:56 -04:00
return m_channelAPI - > getName ( ) < other . m_channelAPI - > getName ( ) ;
2017-12-17 17:15:42 -05:00
}
else
{
2019-09-07 18:44:56 -04:00
return m_channelAPI - > getCenterFrequency ( ) < other . m_channelAPI - > getCenterFrequency ( ) ;
2017-12-17 17:15:42 -05:00
}
}
else
{
return false ;
}
}