2016-05-15 19:12:55 -04:00
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2016 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 //
// //
// 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/>. //
///////////////////////////////////////////////////////////////////////////////////
2017-09-25 18:22:08 -04:00
# include <plugin/plugininstancegui.h>
2016-10-17 18:03:51 -04:00
# include "device/devicesourceapi.h"
2016-12-29 06:41:10 -05:00
# include "device/devicesinkapi.h"
2017-09-17 11:35:03 -04:00
# include "dsp/devicesamplesource.h"
2016-05-16 12:53:01 -04:00
# include "plugin/plugininterface.h"
2016-05-16 11:47:22 -04:00
# include "settings/preset.h"
2017-10-24 10:54:01 -04:00
# include "dsp/dspengine.h"
2017-11-18 21:38:07 -05:00
# include "channel/channelsinkapi.h"
2016-05-16 10:02:55 -04:00
2017-10-24 10:54:01 -04:00
DeviceSourceAPI : : DeviceSourceAPI ( int deviceTabIndex ,
2017-10-31 16:57:29 -04:00
DSPDeviceSourceEngine * deviceSourceEngine ) :
2016-05-16 10:02:55 -04:00
m_deviceTabIndex ( deviceTabIndex ) ,
2016-10-17 18:03:51 -04:00
m_deviceSourceEngine ( deviceSourceEngine ) ,
2016-05-16 10:37:07 -04:00
m_sampleSourceSequence ( 0 ) ,
2017-11-18 19:05:16 -05:00
m_nbItems ( 1 ) ,
2017-11-02 04:17:38 -04:00
m_itemIndex ( 0 ) ,
2017-09-14 19:49:18 -04:00
m_pluginInterface ( 0 ) ,
2017-09-03 14:24:17 -04:00
m_sampleSourcePluginInstanceUI ( 0 ) ,
2017-07-03 02:59:48 -04:00
m_buddySharedPtr ( 0 ) ,
2017-09-14 07:34:32 -04:00
m_isBuddyLeader ( false ) ,
2017-10-24 10:58:12 -04:00
m_masterTimer ( DSPEngine : : instance ( ) - > getMasterTimer ( ) )
2016-05-15 19:12:55 -04:00
{
}
2016-10-10 19:17:55 -04:00
DeviceSourceAPI : : ~ DeviceSourceAPI ( )
2016-05-15 19:12:55 -04:00
{
}
2016-10-10 19:17:55 -04:00
void DeviceSourceAPI : : addSink ( BasebandSampleSink * sink )
2016-05-15 19:12:55 -04:00
{
2016-10-17 18:03:51 -04:00
m_deviceSourceEngine - > addSink ( sink ) ;
2016-05-15 19:12:55 -04:00
}
2016-10-10 19:17:55 -04:00
void DeviceSourceAPI : : removeSink ( BasebandSampleSink * sink )
2016-05-15 19:12:55 -04:00
{
2016-10-17 18:03:51 -04:00
m_deviceSourceEngine - > removeSink ( sink ) ;
2016-05-15 19:12:55 -04:00
}
2016-10-10 19:17:55 -04:00
void DeviceSourceAPI : : addThreadedSink ( ThreadedBasebandSampleSink * sink )
2016-05-15 19:12:55 -04:00
{
2016-10-17 18:03:51 -04:00
m_deviceSourceEngine - > addThreadedSink ( sink ) ;
2016-05-15 19:12:55 -04:00
}
2016-10-10 19:17:55 -04:00
void DeviceSourceAPI : : removeThreadedSink ( ThreadedBasebandSampleSink * sink )
2016-05-15 19:12:55 -04:00
{
2016-10-17 18:03:51 -04:00
m_deviceSourceEngine - > removeThreadedSink ( sink ) ;
2016-05-15 19:12:55 -04:00
}
2017-11-18 21:38:07 -05:00
void DeviceSourceAPI : : addChannelAPI ( ChannelSinkAPI * channelAPI )
{
m_channelAPIs . append ( channelAPI ) ;
renumerateChannels ( ) ;
}
void DeviceSourceAPI : : removeChannelAPI ( ChannelSinkAPI * channelAPI )
{
if ( m_channelAPIs . removeOne ( channelAPI ) ) {
renumerateChannels ( ) ;
}
channelAPI - > setIndexInDeviceSet ( - 1 ) ;
}
ChannelSinkAPI * DeviceSourceAPI : : getChanelAPIAt ( int index )
{
if ( index < m_channelAPIs . size ( ) ) {
return m_channelAPIs . at ( index ) ;
} else {
return 0 ;
}
}
void DeviceSourceAPI : : renumerateChannels ( )
{
for ( int i = 0 ; i < m_channelAPIs . size ( ) ; + + i ) {
m_channelAPIs . at ( i ) - > setIndexInDeviceSet ( i ) ;
}
}
2017-09-15 02:45:22 -04:00
void DeviceSourceAPI : : setSampleSource ( DeviceSampleSource * source )
2016-05-15 19:12:55 -04:00
{
2016-10-17 18:03:51 -04:00
m_deviceSourceEngine - > setSource ( source ) ;
2016-05-15 19:12:55 -04:00
}
2017-09-15 02:45:22 -04:00
DeviceSampleSource * DeviceSourceAPI : : getSampleSource ( )
2017-09-05 07:57:49 -04:00
{
return m_deviceSourceEngine - > getSource ( ) ;
}
2016-10-10 19:17:55 -04:00
bool DeviceSourceAPI : : initAcquisition ( )
2016-05-15 19:12:55 -04:00
{
2016-10-17 18:03:51 -04:00
return m_deviceSourceEngine - > initAcquisition ( ) ;
2016-05-15 19:12:55 -04:00
}
2016-10-10 19:17:55 -04:00
bool DeviceSourceAPI : : startAcquisition ( )
2016-05-15 19:12:55 -04:00
{
2016-10-17 18:03:51 -04:00
return m_deviceSourceEngine - > startAcquisition ( ) ;
2016-05-15 19:12:55 -04:00
}
2016-10-10 19:17:55 -04:00
void DeviceSourceAPI : : stopAcquisition ( )
2016-05-15 19:12:55 -04:00
{
2016-10-17 18:03:51 -04:00
m_deviceSourceEngine - > stopAcquistion ( ) ;
2016-05-15 19:12:55 -04:00
}
2016-10-10 19:17:55 -04:00
DSPDeviceSourceEngine : : State DeviceSourceAPI : : state ( ) const
2016-05-15 19:12:55 -04:00
{
2016-10-17 18:03:51 -04:00
return m_deviceSourceEngine - > state ( ) ;
2016-05-15 19:12:55 -04:00
}
2016-10-10 19:17:55 -04:00
QString DeviceSourceAPI : : errorMessage ( )
2016-05-15 19:12:55 -04:00
{
2016-10-17 18:03:51 -04:00
return m_deviceSourceEngine - > errorMessage ( ) ;
2016-05-15 19:12:55 -04:00
}
2016-10-10 19:17:55 -04:00
uint DeviceSourceAPI : : getDeviceUID ( ) const
2016-05-15 19:12:55 -04:00
{
2016-10-17 18:03:51 -04:00
return m_deviceSourceEngine - > getUID ( ) ;
2016-05-15 19:12:55 -04:00
}
2017-09-13 17:40:06 -04:00
MessageQueue * DeviceSourceAPI : : getDeviceEngineInputMessageQueue ( )
2016-05-15 19:12:55 -04:00
{
2016-10-17 18:03:51 -04:00
return m_deviceSourceEngine - > getInputMessageQueue ( ) ;
2016-05-15 19:12:55 -04:00
}
2017-09-17 11:35:03 -04:00
MessageQueue * DeviceSourceAPI : : getSampleSourceInputMessageQueue ( )
2016-05-15 19:12:55 -04:00
{
2017-09-17 11:35:03 -04:00
return getSampleSource ( ) - > getInputMessageQueue ( ) ;
2016-05-15 19:12:55 -04:00
}
2017-09-17 11:35:03 -04:00
MessageQueue * DeviceSourceAPI : : getSampleSourceGUIMessageQueue ( )
{
return getSampleSource ( ) - > getMessageQueueToGUI ( ) ;
}
2016-10-10 19:17:55 -04:00
void DeviceSourceAPI : : configureCorrections ( bool dcOffsetCorrection , bool iqImbalanceCorrection )
2016-05-15 19:12:55 -04:00
{
2016-10-17 18:03:51 -04:00
m_deviceSourceEngine - > configureCorrections ( dcOffsetCorrection , iqImbalanceCorrection ) ;
2016-05-15 19:12:55 -04:00
}
2016-12-29 06:41:10 -05:00
void DeviceSourceAPI : : setHardwareId ( const QString & id )
{
m_hardwareId = id ;
}
2016-10-10 19:17:55 -04:00
void DeviceSourceAPI : : setSampleSourceId ( const QString & id )
2016-05-16 10:37:07 -04:00
{
m_sampleSourceId = id ;
}
2017-09-15 02:23:04 -04:00
void DeviceSourceAPI : : resetSampleSourceId ( )
{
m_sampleSourceId . clear ( ) ;
}
2016-10-10 19:17:55 -04:00
void DeviceSourceAPI : : setSampleSourceSerial ( const QString & serial )
2016-05-16 10:37:07 -04:00
{
m_sampleSourceSerial = serial ;
}
2017-09-14 19:24:51 -04:00
void DeviceSourceAPI : : setSampleSourceDisplayName ( const QString & name )
{
m_sampleSourceDisplayName = name ;
}
2016-10-10 19:17:55 -04:00
void DeviceSourceAPI : : setSampleSourceSequence ( int sequence )
2016-05-16 10:37:07 -04:00
{
m_sampleSourceSequence = sequence ;
2016-10-17 18:03:51 -04:00
m_deviceSourceEngine - > setSourceSequence ( sequence ) ;
2016-05-16 10:37:07 -04:00
}
2017-11-18 19:05:16 -05:00
void DeviceSourceAPI : : setNbItems ( uint32_t nbItems )
{
m_nbItems = nbItems ;
}
2017-11-02 04:17:38 -04:00
void DeviceSourceAPI : : setItemIndex ( uint32_t index )
{
m_itemIndex = index ;
}
2017-09-15 12:48:30 -04:00
void DeviceSourceAPI : : setSampleSourcePluginInterface ( PluginInterface * iface )
2017-09-14 19:30:25 -04:00
{
2017-09-15 12:48:30 -04:00
m_pluginInterface = iface ;
2017-09-14 19:30:25 -04:00
}
2017-09-25 18:22:08 -04:00
void DeviceSourceAPI : : setSampleSourcePluginInstanceGUI ( PluginInstanceGUI * gui )
2016-05-16 10:37:07 -04:00
{
2017-09-03 14:24:17 -04:00
m_sampleSourcePluginInstanceUI = gui ;
2016-05-16 10:37:07 -04:00
}
2017-11-18 19:05:16 -05:00
void DeviceSourceAPI : : getDeviceEngineStateStr ( QString & state )
{
if ( m_deviceSourceEngine )
{
switch ( m_deviceSourceEngine - > state ( ) )
{
case DSPDeviceSourceEngine : : StNotStarted :
state = " notStarted " ;
break ;
case DSPDeviceSourceEngine : : StIdle :
state = " idle " ;
break ;
case DSPDeviceSourceEngine : : StReady :
state = " ready " ;
break ;
case DSPDeviceSourceEngine : : StRunning :
state = " running " ;
break ;
case DSPDeviceSourceEngine : : StError :
state = " error " ;
break ;
default :
state = " notStarted " ;
break ;
}
}
else
{
state = " notStarted " ;
}
}
2016-10-10 19:17:55 -04:00
void DeviceSourceAPI : : loadSourceSettings ( const Preset * preset )
2016-05-16 11:47:22 -04:00
{
2016-10-18 18:07:42 -04:00
if ( preset - > isSourcePreset ( ) )
2016-05-16 11:47:22 -04:00
{
2016-10-22 04:10:17 -04:00
qDebug ( " DeviceSourceAPI::loadSourceSettings: Loading preset [%s | %s] " , qPrintable ( preset - > getGroup ( ) ) , qPrintable ( preset - > getDescription ( ) ) ) ;
2016-05-16 11:47:22 -04:00
2017-12-27 21:21:48 -05:00
const QByteArray * sourceConfig = preset - > findBestDeviceConfig ( m_sampleSourceId , m_sampleSourceSerial , m_sampleSourceSequence ) ;
qint64 centerFrequency = preset - > getCenterFrequency ( ) ;
2017-12-27 22:04:50 -05:00
qDebug ( " DeviceSourceAPI::loadSourceSettings: center frequency: %llu Hz " , centerFrequency ) ;
2017-12-27 21:21:48 -05:00
if ( sourceConfig ! = 0 )
2016-05-16 11:47:22 -04:00
{
2017-12-27 22:04:50 -05:00
qDebug ( " DeviceSourceAPI::loadSourceSettings: deserializing source %s[%d]: %s " , qPrintable ( m_sampleSourceId ) , m_sampleSourceSequence , qPrintable ( m_sampleSourceSerial ) ) ;
2016-10-18 18:07:42 -04:00
2017-12-27 22:04:50 -05:00
if ( m_sampleSourcePluginInstanceUI ! = 0 ) // GUI flavor
2016-10-18 18:07:42 -04:00
{
2017-09-03 14:24:17 -04:00
m_sampleSourcePluginInstanceUI - > deserialize ( * sourceConfig ) ;
2016-10-18 18:07:42 -04:00
}
2017-12-27 22:04:50 -05:00
else if ( m_deviceSourceEngine - > getSource ( ) ! = 0 ) // Server flavor
2017-09-10 17:38:50 -04:00
{
2017-12-27 21:21:48 -05:00
m_deviceSourceEngine - > getSource ( ) - > deserialize ( * sourceConfig ) ;
2017-09-10 17:38:50 -04:00
}
2017-12-27 22:04:50 -05:00
else
{
2018-01-10 22:40:13 -05:00
qDebug ( " DeviceSourceAPI::loadSourceSettings: deserializing no source " ) ;
2017-12-27 22:04:50 -05:00
}
2017-12-27 21:21:48 -05:00
}
else
{
2017-12-27 22:04:50 -05:00
qDebug ( " DeviceSourceAPI::loadSourceSettings: source %s[%d]: %s not found " , qPrintable ( m_sampleSourceId ) , m_sampleSourceSequence , qPrintable ( m_sampleSourceSerial ) ) ;
2016-10-18 18:07:42 -04:00
}
2018-01-10 22:40:13 -05:00
// set center frequency anyway
if ( m_sampleSourcePluginInstanceUI ! = 0 ) // GUI flavor
{
m_sampleSourcePluginInstanceUI - > setCenterFrequency ( centerFrequency ) ;
}
else if ( m_deviceSourceEngine - > getSource ( ) ! = 0 ) // Server flavor
{
m_deviceSourceEngine - > getSource ( ) - > setCenterFrequency ( centerFrequency ) ;
}
else
{
qDebug ( " DeviceSourceAPI::loadSourceSettings: no source " ) ;
}
2016-10-18 18:07:42 -04:00
}
else
{
2017-11-11 05:11:44 -05:00
qDebug ( " DeviceSourceAPI::loadSourceSettings: Loading preset [%s | %s] is not a source preset " , qPrintable ( preset - > getGroup ( ) ) , qPrintable ( preset - > getDescription ( ) ) ) ;
2016-05-16 11:47:22 -04:00
}
}
2016-10-10 19:17:55 -04:00
void DeviceSourceAPI : : saveSourceSettings ( Preset * preset )
2016-05-16 11:47:22 -04:00
{
2016-10-18 18:07:42 -04:00
if ( preset - > isSourcePreset ( ) )
{
2017-12-28 11:22:18 -05:00
qDebug ( " DeviceSourceAPI::saveSourceSettings: serializing source %s[%d]: %s " , qPrintable ( m_sampleSourceId ) , m_sampleSourceSequence , qPrintable ( m_sampleSourceSerial ) ) ;
2017-12-27 21:21:48 -05:00
if ( m_sampleSourcePluginInstanceUI ! = 0 )
2016-10-18 18:07:42 -04:00
{
2017-09-03 14:24:17 -04:00
preset - > addOrUpdateDeviceConfig ( m_sampleSourceId , m_sampleSourceSerial , m_sampleSourceSequence , m_sampleSourcePluginInstanceUI - > serialize ( ) ) ;
preset - > setCenterFrequency ( m_sampleSourcePluginInstanceUI - > getCenterFrequency ( ) ) ;
2016-10-18 18:07:42 -04:00
}
2017-12-27 22:04:50 -05:00
else if ( m_deviceSourceEngine - > getSource ( ) ! = 0 )
2016-10-22 04:10:17 -04:00
{
2017-12-27 21:21:48 -05:00
preset - > addOrUpdateDeviceConfig ( m_sampleSourceId , m_sampleSourceSerial , m_sampleSourceSequence , m_deviceSourceEngine - > getSource ( ) - > serialize ( ) ) ;
preset - > setCenterFrequency ( m_deviceSourceEngine - > getSource ( ) - > getCenterFrequency ( ) ) ;
2016-10-22 04:10:17 -04:00
}
2017-12-27 22:04:50 -05:00
else
{
qDebug ( " DeviceSourceAPI::saveSourceSettings: no source " ) ;
}
2016-10-18 18:07:42 -04:00
}
else
2016-05-16 11:47:22 -04:00
{
2016-10-22 04:10:17 -04:00
qDebug ( " DeviceSourceAPI::saveSourceSettings: not a source preset " ) ;
2016-05-16 11:47:22 -04:00
}
}
2016-12-29 06:41:10 -05:00
void DeviceSourceAPI : : addSourceBuddy ( DeviceSourceAPI * buddy )
{
m_sourceBuddies . push_back ( buddy ) ;
2016-12-29 13:26:45 -05:00
buddy - > m_sourceBuddies . push_back ( this ) ;
qDebug ( " DeviceSourceAPI::addSourceBuddy: added buddy %s(%s) [%lx] <-> [%lx] " ,
2016-12-29 06:41:10 -05:00
qPrintable ( buddy - > getHardwareId ( ) ) ,
2016-12-29 13:26:45 -05:00
qPrintable ( buddy - > getSampleSourceSerial ( ) ) ,
( uint64_t ) buddy ,
( uint64_t ) this ) ;
2016-12-29 06:41:10 -05:00
}
void DeviceSourceAPI : : addSinkBuddy ( DeviceSinkAPI * buddy )
{
m_sinkBuddies . push_back ( buddy ) ;
2017-01-02 17:30:50 -05:00
buddy - > m_sourceBuddies . push_back ( this ) ;
2016-12-29 13:26:45 -05:00
qDebug ( " DeviceSourceAPI::addSinkBuddy: added buddy %s(%s) [%lx] <-> [%lx] " ,
2016-12-29 06:41:10 -05:00
qPrintable ( buddy - > getHardwareId ( ) ) ,
2016-12-29 13:26:45 -05:00
qPrintable ( buddy - > getSampleSinkSerial ( ) ) ,
( uint64_t ) buddy ,
( uint64_t ) this ) ;
2016-12-29 06:41:10 -05:00
}
void DeviceSourceAPI : : removeSourceBuddy ( DeviceSourceAPI * buddy )
{
std : : vector < DeviceSourceAPI * > : : iterator it = m_sourceBuddies . begin ( ) ;
for ( ; it ! = m_sourceBuddies . end ( ) ; + + it )
{
2016-12-29 13:26:45 -05:00
if ( * it = = buddy )
2016-12-29 06:41:10 -05:00
{
2016-12-29 13:26:45 -05:00
qDebug ( " DeviceSourceAPI::removeSourceBuddy: buddy %s(%s) [%lx] removed from the list of [%lx] " ,
2016-12-29 06:41:10 -05:00
qPrintable ( buddy - > getHardwareId ( ) ) ,
2016-12-29 13:26:45 -05:00
qPrintable ( buddy - > getSampleSourceSerial ( ) ) ,
( uint64_t ) ( * it ) ,
( uint64_t ) this ) ;
m_sourceBuddies . erase ( it ) ;
2016-12-29 06:41:10 -05:00
return ;
}
}
2016-12-29 13:26:45 -05:00
qDebug ( " DeviceSourceAPI::removeSourceBuddy: buddy %s(%s) [%lx] not found in the list of [%lx] " ,
2016-12-29 06:41:10 -05:00
qPrintable ( buddy - > getHardwareId ( ) ) ,
2016-12-29 13:26:45 -05:00
qPrintable ( buddy - > getSampleSourceSerial ( ) ) ,
( uint64_t ) buddy ,
( uint64_t ) this ) ;
2016-12-29 06:41:10 -05:00
}
void DeviceSourceAPI : : removeSinkBuddy ( DeviceSinkAPI * buddy )
{
std : : vector < DeviceSinkAPI * > : : iterator it = m_sinkBuddies . begin ( ) ;
for ( ; it ! = m_sinkBuddies . end ( ) ; + + it )
{
2016-12-29 13:26:45 -05:00
if ( * it = = buddy )
2016-12-29 06:41:10 -05:00
{
2016-12-29 13:26:45 -05:00
qDebug ( " DeviceSourceAPI::removeSinkBuddy: buddy %s(%s) [%lx] removed from the list of [%lx] " ,
2016-12-29 06:41:10 -05:00
qPrintable ( buddy - > getHardwareId ( ) ) ,
2016-12-29 13:26:45 -05:00
qPrintable ( buddy - > getSampleSinkSerial ( ) ) ,
( uint64_t ) ( * it ) ,
( uint64_t ) this ) ;
m_sinkBuddies . erase ( it ) ;
2016-12-29 06:41:10 -05:00
return ;
}
}
2016-12-29 13:26:45 -05:00
qDebug ( " DeviceSourceAPI::removeSinkBuddy: buddy %s(%s) [%lx] not found in the list of [%lx] " ,
2016-12-29 06:41:10 -05:00
qPrintable ( buddy - > getHardwareId ( ) ) ,
2016-12-29 13:26:45 -05:00
qPrintable ( buddy - > getSampleSinkSerial ( ) ) ,
( uint64_t ) buddy ,
( uint64_t ) this ) ;
2016-12-29 06:41:10 -05:00
}
2016-12-29 13:26:45 -05:00
void DeviceSourceAPI : : clearBuddiesLists ( )
2016-12-29 06:41:10 -05:00
{
std : : vector < DeviceSourceAPI * > : : iterator itSource = m_sourceBuddies . begin ( ) ;
std : : vector < DeviceSinkAPI * > : : iterator itSink = m_sinkBuddies . begin ( ) ;
2017-07-03 02:59:48 -04:00
bool leaderElected = false ;
2016-12-29 06:41:10 -05:00
for ( ; itSource ! = m_sourceBuddies . end ( ) ; + + itSource )
{
2017-07-03 02:59:48 -04:00
if ( isBuddyLeader ( ) & & ! leaderElected )
{
( * itSource ) - > setBuddyLeader ( true ) ;
leaderElected = true ;
}
2016-12-29 06:41:10 -05:00
( * itSource ) - > removeSourceBuddy ( this ) ;
}
2016-12-29 13:26:45 -05:00
m_sourceBuddies . clear ( ) ;
2016-12-29 06:41:10 -05:00
for ( ; itSink ! = m_sinkBuddies . end ( ) ; + + itSink )
{
2017-07-03 02:59:48 -04:00
if ( isBuddyLeader ( ) & & ! leaderElected )
{
( * itSink ) - > setBuddyLeader ( true ) ;
leaderElected = true ;
}
2016-12-29 06:41:10 -05:00
( * itSink ) - > removeSourceBuddy ( this ) ;
}
2016-12-29 13:26:45 -05:00
m_sinkBuddies . clear ( ) ;
2016-12-29 06:41:10 -05:00
}