2015-10-15 02:05:19 +00:00
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2015 F4EXB //
// written by Edouard Griffiths //
// //
// 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/>. //
///////////////////////////////////////////////////////////////////////////////////
2016-10-03 16:29:05 +00:00
# include "dspdevicesourceengine.h"
2016-10-02 20:29:04 +00:00
# include <dsp/basebandsamplesink.h>
2016-10-02 21:16:40 +00:00
# include <dsp/devicesamplesource.h>
2016-10-02 19:52:39 +00:00
# include <dsp/downchannelizer.h>
2015-10-15 02:05:19 +00:00
# include <stdio.h>
# include <QDebug>
# include "dsp/dspcommands.h"
2016-10-06 17:18:02 +00:00
# include "samplesinkfifo.h"
2016-10-03 13:55:16 +00:00
# include "threadedbasebandsamplesink.h"
2015-10-15 02:05:19 +00:00
2016-10-03 16:29:05 +00:00
DSPDeviceSourceEngine : : DSPDeviceSourceEngine ( uint uid , QObject * parent ) :
2015-10-15 02:05:19 +00:00
QThread ( parent ) ,
2017-05-25 18:13:34 +00:00
m_uid ( uid ) ,
2015-10-15 02:05:19 +00:00
m_state ( StNotStarted ) ,
2016-10-03 23:49:28 +00:00
m_deviceSampleSource ( 0 ) ,
2015-10-15 02:05:19 +00:00
m_sampleSourceSequence ( 0 ) ,
2016-10-03 23:49:28 +00:00
m_basebandSampleSinks ( ) ,
2015-10-15 02:05:19 +00:00
m_sampleRate ( 0 ) ,
m_centerFrequency ( 0 ) ,
m_dcOffsetCorrection ( false ) ,
m_iqImbalanceCorrection ( false ) ,
m_iOffset ( 0 ) ,
m_qOffset ( 0 ) ,
m_iRange ( 1 < < 16 ) ,
m_qRange ( 1 < < 16 ) ,
m_imbalance ( 65536 )
{
2015-10-21 00:32:21 +00:00
connect ( & m_inputMessageQueue , SIGNAL ( messageEnqueued ( ) ) , this , SLOT ( handleInputMessages ( ) ) , Qt : : QueuedConnection ) ;
connect ( & m_syncMessenger , SIGNAL ( messageSent ( ) ) , this , SLOT ( handleSynchronousMessages ( ) ) , Qt : : QueuedConnection ) ;
2015-10-15 02:05:19 +00:00
moveToThread ( this ) ;
}
2016-10-03 16:29:05 +00:00
DSPDeviceSourceEngine : : ~ DSPDeviceSourceEngine ( )
2015-10-15 02:05:19 +00:00
{
wait ( ) ;
}
2016-10-03 16:29:05 +00:00
void DSPDeviceSourceEngine : : run ( )
2015-10-15 02:05:19 +00:00
{
2016-10-08 08:35:07 +00:00
qDebug ( ) < < " DSPDeviceSourceEngine::run " ;
2015-10-15 02:05:19 +00:00
m_state = StIdle ;
2015-10-21 00:32:21 +00:00
m_syncMessenger . done ( ) ; // Release start() that is waiting in main thread
2015-10-15 02:05:19 +00:00
exec ( ) ;
}
2016-10-03 16:29:05 +00:00
void DSPDeviceSourceEngine : : start ( )
2015-10-15 02:05:19 +00:00
{
2016-10-08 08:35:07 +00:00
qDebug ( ) < < " DSPDeviceSourceEngine::start " ;
2015-10-15 02:05:19 +00:00
QThread : : start ( ) ;
}
2016-10-03 16:29:05 +00:00
void DSPDeviceSourceEngine : : stop ( )
2015-10-15 02:05:19 +00:00
{
2016-10-08 08:35:07 +00:00
qDebug ( ) < < " DSPDeviceSourceEngine::stop " ;
2017-10-03 22:50:42 +00:00
gotoIdle ( ) ;
m_state = StNotStarted ;
QThread : : exit ( ) ;
// DSPExit cmd;
// m_syncMessenger.sendWait(cmd);
2015-10-15 02:05:19 +00:00
}
2016-10-03 16:29:05 +00:00
bool DSPDeviceSourceEngine : : initAcquisition ( )
2015-10-15 02:05:19 +00:00
{
2016-10-08 08:35:07 +00:00
qDebug ( ) < < " DSPDeviceSourceEngine::initAcquisition " ;
2015-10-15 02:05:19 +00:00
DSPAcquisitionInit cmd ;
return m_syncMessenger . sendWait ( cmd ) = = StReady ;
}
2016-10-03 16:29:05 +00:00
bool DSPDeviceSourceEngine : : startAcquisition ( )
2015-10-15 02:05:19 +00:00
{
2016-10-08 08:35:07 +00:00
qDebug ( ) < < " DSPDeviceSourceEngine::startAcquisition " ;
2015-10-15 02:05:19 +00:00
DSPAcquisitionStart cmd ;
return m_syncMessenger . sendWait ( cmd ) = = StRunning ;
}
2016-10-03 16:29:05 +00:00
void DSPDeviceSourceEngine : : stopAcquistion ( )
2015-10-15 02:05:19 +00:00
{
2016-10-08 08:35:07 +00:00
qDebug ( ) < < " DSPDeviceSourceEngine::stopAcquistion " ;
2015-10-15 02:05:19 +00:00
DSPAcquisitionStop cmd ;
m_syncMessenger . storeMessage ( cmd ) ;
handleSynchronousMessages ( ) ;
if ( m_dcOffsetCorrection )
{
qDebug ( " DC offset:%f,%f " , m_iOffset , m_qOffset ) ;
}
}
2016-10-03 16:29:05 +00:00
void DSPDeviceSourceEngine : : setSource ( DeviceSampleSource * source )
2015-10-15 02:05:19 +00:00
{
2016-10-08 08:35:07 +00:00
qDebug ( ) < < " DSPDeviceSourceEngine::setSource " ;
2015-10-15 02:05:19 +00:00
DSPSetSource cmd ( source ) ;
m_syncMessenger . sendWait ( cmd ) ;
}
2016-10-03 16:29:05 +00:00
void DSPDeviceSourceEngine : : setSourceSequence ( int sequence )
2015-10-15 02:05:19 +00:00
{
2016-10-08 08:35:07 +00:00
qDebug ( " DSPDeviceSourceEngine::setSourceSequence: seq: %d " , sequence ) ;
2015-10-15 02:05:19 +00:00
m_sampleSourceSequence = sequence ;
}
2016-10-03 16:29:05 +00:00
void DSPDeviceSourceEngine : : addSink ( BasebandSampleSink * sink )
2015-10-15 02:05:19 +00:00
{
2016-10-08 08:35:07 +00:00
qDebug ( ) < < " DSPDeviceSourceEngine::addSink: " < < sink - > objectName ( ) . toStdString ( ) . c_str ( ) ;
2015-10-15 02:05:19 +00:00
DSPAddSink cmd ( sink ) ;
m_syncMessenger . sendWait ( cmd ) ;
}
2016-10-03 16:29:05 +00:00
void DSPDeviceSourceEngine : : removeSink ( BasebandSampleSink * sink )
2015-10-15 02:05:19 +00:00
{
2016-10-08 08:35:07 +00:00
qDebug ( ) < < " DSPDeviceSourceEngine::removeSink: " < < sink - > objectName ( ) . toStdString ( ) . c_str ( ) ;
2015-10-15 02:05:19 +00:00
DSPRemoveSink cmd ( sink ) ;
m_syncMessenger . sendWait ( cmd ) ;
}
2016-10-03 16:29:05 +00:00
void DSPDeviceSourceEngine : : addThreadedSink ( ThreadedBasebandSampleSink * sink )
2015-10-15 02:05:19 +00:00
{
2016-10-08 08:35:07 +00:00
qDebug ( ) < < " DSPDeviceSourceEngine::addThreadedSink: " < < sink - > objectName ( ) . toStdString ( ) . c_str ( ) ;
2015-10-15 02:05:19 +00:00
DSPAddThreadedSampleSink cmd ( sink ) ;
m_syncMessenger . sendWait ( cmd ) ;
}
2016-10-03 16:29:05 +00:00
void DSPDeviceSourceEngine : : removeThreadedSink ( ThreadedBasebandSampleSink * sink )
2015-10-15 02:05:19 +00:00
{
2016-10-08 08:35:07 +00:00
qDebug ( ) < < " DSPDeviceSourceEngine::removeThreadedSink: " < < sink - > objectName ( ) . toStdString ( ) . c_str ( ) ;
2015-10-15 02:05:19 +00:00
DSPRemoveThreadedSampleSink cmd ( sink ) ;
m_syncMessenger . sendWait ( cmd ) ;
}
2016-10-03 16:29:05 +00:00
void DSPDeviceSourceEngine : : configureCorrections ( bool dcOffsetCorrection , bool iqImbalanceCorrection )
2015-10-15 02:05:19 +00:00
{
2016-10-08 08:35:07 +00:00
qDebug ( ) < < " DSPDeviceSourceEngine::configureCorrections " ;
2015-10-15 02:05:19 +00:00
DSPConfigureCorrection * cmd = new DSPConfigureCorrection ( dcOffsetCorrection , iqImbalanceCorrection ) ;
m_inputMessageQueue . push ( cmd ) ;
}
2016-10-03 16:29:05 +00:00
QString DSPDeviceSourceEngine : : errorMessage ( )
2015-10-15 02:05:19 +00:00
{
2016-10-08 08:35:07 +00:00
qDebug ( ) < < " DSPDeviceSourceEngine::errorMessage " ;
2015-10-15 02:05:19 +00:00
DSPGetErrorMessage cmd ;
m_syncMessenger . sendWait ( cmd ) ;
return cmd . getErrorMessage ( ) ;
}
2016-10-03 16:29:05 +00:00
QString DSPDeviceSourceEngine : : sourceDeviceDescription ( )
2015-10-15 02:05:19 +00:00
{
2016-10-08 08:35:07 +00:00
qDebug ( ) < < " DSPDeviceSourceEngine::sourceDeviceDescription " ;
2015-10-15 02:05:19 +00:00
DSPGetSourceDeviceDescription cmd ;
m_syncMessenger . sendWait ( cmd ) ;
return cmd . getDeviceDescription ( ) ;
}
2016-10-03 16:29:05 +00:00
void DSPDeviceSourceEngine : : dcOffset ( SampleVector : : iterator begin , SampleVector : : iterator end )
2015-10-15 02:05:19 +00:00
{
double count ;
int io = 0 ;
int qo = 0 ;
Sample corr ( ( qint16 ) m_iOffset , ( qint16 ) m_qOffset ) ;
// sum and correct in one pass
for ( SampleVector : : iterator it = begin ; it < end ; it + + )
{
io + = it - > real ( ) ;
qo + = it - > imag ( ) ;
* it - = corr ;
}
// moving average
count = end - begin ;
m_iOffset = ( 15.0 * m_iOffset + ( double ) io / count ) / 16.0 ;
m_qOffset = ( 15.0 * m_qOffset + ( double ) qo / count ) / 16.0 ;
}
2016-10-03 16:29:05 +00:00
void DSPDeviceSourceEngine : : imbalance ( SampleVector : : iterator begin , SampleVector : : iterator end )
2015-10-15 02:05:19 +00:00
{
int iMin = 0 ;
int iMax = 0 ;
int qMin = 0 ;
int qMax = 0 ;
// find value ranges for both I and Q
// both intervals should be same same size (for a perfect circle)
for ( SampleVector : : iterator it = begin ; it < end ; it + + )
{
if ( it ! = begin )
{
if ( it - > real ( ) < iMin ) {
iMin = it - > real ( ) ;
} else if ( it - > real ( ) > iMax ) {
iMax = it - > real ( ) ;
}
if ( it - > imag ( ) < qMin ) {
qMin = it - > imag ( ) ;
} else if ( it - > imag ( ) > qMax ) {
qMax = it - > imag ( ) ;
}
}
else
{
iMin = it - > real ( ) ;
iMax = it - > real ( ) ;
qMin = it - > imag ( ) ;
qMax = it - > imag ( ) ;
}
}
// sliding average (el cheapo again)
m_iRange = ( m_iRange * 15 + ( iMax - iMin ) ) > > 4 ;
m_qRange = ( m_qRange * 15 + ( qMax - qMin ) ) > > 4 ;
// calculate imbalance as Q15.16
if ( m_qRange ! = 0 ) {
m_imbalance = ( ( uint ) m_iRange < < 16 ) / ( uint ) m_qRange ;
}
// correct imbalance and convert back to signed int 16
for ( SampleVector : : iterator it = begin ; it < end ; it + + ) {
it - > m_imag = ( it - > m_imag * m_imbalance ) > > 16 ;
}
}
2016-10-03 16:29:05 +00:00
void DSPDeviceSourceEngine : : work ( )
2015-10-15 02:05:19 +00:00
{
2016-10-06 17:18:02 +00:00
SampleSinkFifo * sampleFifo = m_deviceSampleSource - > getSampleFifo ( ) ;
2015-10-15 02:05:19 +00:00
std : : size_t samplesDone = 0 ;
bool positiveOnly = false ;
while ( ( sampleFifo - > fill ( ) > 0 ) & & ( m_inputMessageQueue . size ( ) = = 0 ) & & ( samplesDone < m_sampleRate ) )
{
SampleVector : : iterator part1begin ;
SampleVector : : iterator part1end ;
SampleVector : : iterator part2begin ;
SampleVector : : iterator part2end ;
std : : size_t count = sampleFifo - > readBegin ( sampleFifo - > fill ( ) , & part1begin , & part1end , & part2begin , & part2end ) ;
// first part of FIFO data
if ( part1begin ! = part1end )
{
// correct stuff
if ( m_dcOffsetCorrection )
{
dcOffset ( part1begin , part1end ) ;
}
if ( m_iqImbalanceCorrection )
{
imbalance ( part1begin , part1end ) ;
}
// feed data to direct sinks
2016-10-03 23:49:28 +00:00
for ( BasebandSampleSinks : : const_iterator it = m_basebandSampleSinks . begin ( ) ; it ! = m_basebandSampleSinks . end ( ) ; + + it )
2015-10-15 02:05:19 +00:00
{
( * it ) - > feed ( part1begin , part1end , positiveOnly ) ;
}
// feed data to threaded sinks
2016-10-03 23:49:28 +00:00
for ( ThreadedBasebandSampleSinks : : const_iterator it = m_threadedBasebandSampleSinks . begin ( ) ; it ! = m_threadedBasebandSampleSinks . end ( ) ; + + it )
2015-10-15 02:05:19 +00:00
{
( * it ) - > feed ( part1begin , part1end , positiveOnly ) ;
}
}
// second part of FIFO data (used when block wraps around)
if ( part2begin ! = part2end )
{
// correct stuff
if ( m_dcOffsetCorrection )
{
dcOffset ( part2begin , part2end ) ;
}
if ( m_iqImbalanceCorrection )
{
imbalance ( part2begin , part2end ) ;
}
// feed data to direct sinks
2016-10-03 23:49:28 +00:00
for ( BasebandSampleSinks : : const_iterator it = m_basebandSampleSinks . begin ( ) ; it ! = m_basebandSampleSinks . end ( ) ; it + + )
2015-10-15 02:05:19 +00:00
{
( * it ) - > feed ( part2begin , part2end , positiveOnly ) ;
}
// feed data to threaded sinks
2016-10-03 23:49:28 +00:00
for ( ThreadedBasebandSampleSinks : : const_iterator it = m_threadedBasebandSampleSinks . begin ( ) ; it ! = m_threadedBasebandSampleSinks . end ( ) ; + + it )
2015-10-15 02:05:19 +00:00
{
( * it ) - > feed ( part2begin , part2end , positiveOnly ) ;
}
}
// adjust FIFO pointers
sampleFifo - > readCommit ( ( unsigned int ) count ) ;
samplesDone + = count ;
}
}
// notStarted -> idle -> init -> running -+
// ^ |
// +-----------------------+
2016-10-03 16:29:05 +00:00
DSPDeviceSourceEngine : : State DSPDeviceSourceEngine : : gotoIdle ( )
2015-10-15 02:05:19 +00:00
{
2016-10-08 08:35:07 +00:00
qDebug ( ) < < " DSPDeviceSourceEngine::gotoIdle " ;
2015-10-15 02:05:19 +00:00
switch ( m_state ) {
case StNotStarted :
return StNotStarted ;
case StIdle :
case StError :
return StIdle ;
case StReady :
case StRunning :
break ;
}
2016-10-03 23:49:28 +00:00
if ( m_deviceSampleSource = = 0 )
2015-10-15 02:05:19 +00:00
{
return StIdle ;
}
// stop everything
2016-10-03 23:49:28 +00:00
for ( BasebandSampleSinks : : const_iterator it = m_basebandSampleSinks . begin ( ) ; it ! = m_basebandSampleSinks . end ( ) ; it + + )
2015-10-15 02:05:19 +00:00
{
( * it ) - > stop ( ) ;
}
2016-10-17 20:40:02 +00:00
for ( ThreadedBasebandSampleSinks : : const_iterator it = m_threadedBasebandSampleSinks . begin ( ) ; it ! = m_threadedBasebandSampleSinks . end ( ) ; it + + )
{
( * it ) - > stop ( ) ;
}
2016-10-17 16:15:08 +00:00
2016-10-03 23:49:28 +00:00
m_deviceSampleSource - > stop ( ) ;
2015-10-15 02:05:19 +00:00
m_deviceDescription . clear ( ) ;
m_sampleRate = 0 ;
return StIdle ;
}
2016-10-03 16:29:05 +00:00
DSPDeviceSourceEngine : : State DSPDeviceSourceEngine : : gotoInit ( )
2015-10-15 02:05:19 +00:00
{
2016-10-08 08:35:07 +00:00
qDebug ( ) < < " DSPDeviceSourceEngine::gotoInit " ;
2015-10-15 02:05:19 +00:00
switch ( m_state ) {
case StNotStarted :
return StNotStarted ;
case StRunning : // FIXME: assumes it goes first through idle state. Could we get back to init from running directly?
return StRunning ;
case StReady :
return StReady ;
case StIdle :
case StError :
break ;
}
2016-10-03 23:49:28 +00:00
if ( m_deviceSampleSource = = 0 )
2015-10-15 02:05:19 +00:00
{
return gotoError ( " No sample source configured " ) ;
}
// init: pass sample rate and center frequency to all sample rate and/or center frequency dependent sinks and wait for completion
m_iOffset = 0 ;
m_qOffset = 0 ;
m_iRange = 1 < < 16 ;
m_qRange = 1 < < 16 ;
2016-10-03 23:49:28 +00:00
m_deviceDescription = m_deviceSampleSource - > getDeviceDescription ( ) ;
m_centerFrequency = m_deviceSampleSource - > getCenterFrequency ( ) ;
m_sampleRate = m_deviceSampleSource - > getSampleRate ( ) ;
2015-10-15 02:05:19 +00:00
2016-10-08 08:35:07 +00:00
qDebug ( ) < < " DSPDeviceSourceEngine::gotoInit: " < < m_deviceDescription . toStdString ( ) . c_str ( ) < < " : "
2015-10-15 02:05:19 +00:00
< < " sampleRate: " < < m_sampleRate
< < " centerFrequency: " < < m_centerFrequency ;
DSPSignalNotification notif ( m_sampleRate , m_centerFrequency ) ;
2016-10-03 23:49:28 +00:00
for ( BasebandSampleSinks : : const_iterator it = m_basebandSampleSinks . begin ( ) ; it ! = m_basebandSampleSinks . end ( ) ; + + it )
2015-10-15 02:05:19 +00:00
{
2016-10-08 08:35:07 +00:00
qDebug ( ) < < " DSPDeviceSourceEngine::gotoInit: initializing " < < ( * it ) - > objectName ( ) . toStdString ( ) . c_str ( ) ;
2015-10-15 02:05:19 +00:00
( * it ) - > handleMessage ( notif ) ;
}
2016-10-03 23:49:28 +00:00
for ( ThreadedBasebandSampleSinks : : const_iterator it = m_threadedBasebandSampleSinks . begin ( ) ; it ! = m_threadedBasebandSampleSinks . end ( ) ; + + it )
2015-10-15 02:05:19 +00:00
{
2016-10-08 08:35:07 +00:00
qDebug ( ) < < " DSPDeviceSourceEngine::gotoInit: initializing ThreadedSampleSink( " < < ( * it ) - > getSampleSinkObjectName ( ) . toStdString ( ) . c_str ( ) < < " ) " ;
2015-10-15 02:05:19 +00:00
( * it ) - > handleSinkMessage ( notif ) ;
}
// pass data to listeners
2017-09-17 15:35:03 +00:00
if ( m_deviceSampleSource - > getMessageQueueToGUI ( ) )
{
DSPSignalNotification * rep = new DSPSignalNotification ( notif ) ; // make a copy for the output queue
m_deviceSampleSource - > getMessageQueueToGUI ( ) - > push ( rep ) ;
}
2015-10-15 02:05:19 +00:00
return StReady ;
}
2016-10-03 16:29:05 +00:00
DSPDeviceSourceEngine : : State DSPDeviceSourceEngine : : gotoRunning ( )
2015-10-15 02:05:19 +00:00
{
2016-10-08 08:35:07 +00:00
qDebug ( ) < < " DSPDeviceSourceEngine::gotoRunning " ;
2015-10-15 02:05:19 +00:00
switch ( m_state )
{
case StNotStarted :
return StNotStarted ;
case StIdle :
return StIdle ;
case StRunning :
return StRunning ;
case StReady :
case StError :
break ;
}
2016-10-03 23:49:28 +00:00
if ( m_deviceSampleSource = = NULL ) {
2016-10-08 08:35:07 +00:00
return gotoError ( " DSPDeviceSourceEngine::gotoRunning: No sample source configured " ) ;
2015-10-15 02:05:19 +00:00
}
2016-10-08 08:35:07 +00:00
qDebug ( ) < < " DSPDeviceSourceEngine::gotoRunning: " < < m_deviceDescription . toStdString ( ) . c_str ( ) < < " started " ;
2015-10-15 02:05:19 +00:00
// Start everything
2017-04-14 01:40:45 +00:00
if ( ! m_deviceSampleSource - > start ( ) )
2015-10-15 02:05:19 +00:00
{
return gotoError ( " Could not start sample source " ) ;
}
2016-10-03 23:49:28 +00:00
for ( BasebandSampleSinks : : const_iterator it = m_basebandSampleSinks . begin ( ) ; it ! = m_basebandSampleSinks . end ( ) ; it + + )
2015-10-15 02:05:19 +00:00
{
2016-10-08 08:35:07 +00:00
qDebug ( ) < < " DSPDeviceSourceEngine::gotoRunning: starting " < < ( * it ) - > objectName ( ) . toStdString ( ) . c_str ( ) ;
2015-10-15 02:05:19 +00:00
( * it ) - > start ( ) ;
}
2016-10-03 23:49:28 +00:00
for ( ThreadedBasebandSampleSinks : : const_iterator it = m_threadedBasebandSampleSinks . begin ( ) ; it ! = m_threadedBasebandSampleSinks . end ( ) ; + + it )
2015-10-15 02:05:19 +00:00
{
2016-10-08 08:35:07 +00:00
qDebug ( ) < < " DSPDeviceSourceEngine::gotoRunning: starting ThreadedSampleSink( " < < ( * it ) - > getSampleSinkObjectName ( ) . toStdString ( ) . c_str ( ) < < " ) " ;
2015-10-15 02:05:19 +00:00
( * it ) - > start ( ) ;
}
2016-10-08 08:35:07 +00:00
qDebug ( ) < < " DSPDeviceSourceEngine::gotoRunning:input message queue pending: " < < m_inputMessageQueue . size ( ) ;
2015-10-15 02:05:19 +00:00
return StRunning ;
}
2016-10-03 16:29:05 +00:00
DSPDeviceSourceEngine : : State DSPDeviceSourceEngine : : gotoError ( const QString & errorMessage )
2015-10-15 02:05:19 +00:00
{
2016-11-16 01:38:21 +00:00
qDebug ( ) < < " DSPDeviceSourceEngine::gotoError: " < < errorMessage ;
2015-10-15 02:05:19 +00:00
m_errorMessage = errorMessage ;
m_deviceDescription . clear ( ) ;
m_state = StError ;
return StError ;
}
2016-10-03 16:29:05 +00:00
void DSPDeviceSourceEngine : : handleSetSource ( DeviceSampleSource * source )
2015-10-15 02:05:19 +00:00
{
gotoIdle ( ) ;
2016-05-19 22:49:55 +00:00
// if(m_sampleSource != 0)
// {
// disconnect(m_sampleSource->getSampleFifo(), SIGNAL(dataReady()), this, SLOT(handleData()));
// }
2015-10-15 02:05:19 +00:00
2016-10-03 23:49:28 +00:00
m_deviceSampleSource = source ;
2015-10-15 02:05:19 +00:00
2016-10-03 23:49:28 +00:00
if ( m_deviceSampleSource ! = 0 )
2015-10-15 02:05:19 +00:00
{
2016-10-08 08:35:07 +00:00
qDebug ( " DSPDeviceSourceEngine::handleSetSource: set %s " , qPrintable ( source - > getDeviceDescription ( ) ) ) ;
2016-10-03 23:49:28 +00:00
connect ( m_deviceSampleSource - > getSampleFifo ( ) , SIGNAL ( dataReady ( ) ) , this , SLOT ( handleData ( ) ) , Qt : : QueuedConnection ) ;
2015-10-15 02:05:19 +00:00
}
else
{
2016-10-08 08:35:07 +00:00
qDebug ( " DSPDeviceSourceEngine::handleSetSource: set none " ) ;
2015-10-15 02:05:19 +00:00
}
}
2016-10-03 16:29:05 +00:00
void DSPDeviceSourceEngine : : handleData ( )
2015-10-15 02:05:19 +00:00
{
if ( m_state = = StRunning )
{
work ( ) ;
}
}
2016-10-03 16:29:05 +00:00
void DSPDeviceSourceEngine : : handleSynchronousMessages ( )
2015-10-15 02:05:19 +00:00
{
Message * message = m_syncMessenger . getMessage ( ) ;
2016-10-08 08:35:07 +00:00
qDebug ( ) < < " DSPDeviceSourceEngine::handleSynchronousMessages: " < < message - > getIdentifier ( ) ;
2015-10-15 02:05:19 +00:00
if ( DSPExit : : match ( * message ) )
{
gotoIdle ( ) ;
m_state = StNotStarted ;
exit ( ) ;
}
else if ( DSPAcquisitionInit : : match ( * message ) )
{
m_state = gotoIdle ( ) ;
if ( m_state = = StIdle ) {
m_state = gotoInit ( ) ; // State goes ready if init is performed
}
}
else if ( DSPAcquisitionStart : : match ( * message ) )
{
if ( m_state = = StReady ) {
m_state = gotoRunning ( ) ;
}
}
else if ( DSPAcquisitionStop : : match ( * message ) )
{
m_state = gotoIdle ( ) ;
}
else if ( DSPGetSourceDeviceDescription : : match ( * message ) )
{
( ( DSPGetSourceDeviceDescription * ) message ) - > setDeviceDescription ( m_deviceDescription ) ;
}
else if ( DSPGetErrorMessage : : match ( * message ) )
{
( ( DSPGetErrorMessage * ) message ) - > setErrorMessage ( m_errorMessage ) ;
}
else if ( DSPSetSource : : match ( * message ) ) {
handleSetSource ( ( ( DSPSetSource * ) message ) - > getSampleSource ( ) ) ;
}
else if ( DSPAddSink : : match ( * message ) )
{
2016-10-02 20:29:04 +00:00
BasebandSampleSink * sink = ( ( DSPAddSink * ) message ) - > getSampleSink ( ) ;
2016-10-03 23:49:28 +00:00
m_basebandSampleSinks . push_back ( sink ) ;
2015-10-15 02:05:19 +00:00
}
else if ( DSPRemoveSink : : match ( * message ) )
{
2016-10-02 20:29:04 +00:00
BasebandSampleSink * sink = ( ( DSPRemoveSink * ) message ) - > getSampleSink ( ) ;
2015-10-15 02:05:19 +00:00
if ( m_state = = StRunning ) {
sink - > stop ( ) ;
}
2016-10-03 23:49:28 +00:00
m_basebandSampleSinks . remove ( sink ) ;
2015-10-15 02:05:19 +00:00
}
else if ( DSPAddThreadedSampleSink : : match ( * message ) )
{
2016-10-03 13:55:16 +00:00
ThreadedBasebandSampleSink * threadedSink = ( ( DSPAddThreadedSampleSink * ) message ) - > getThreadedSampleSink ( ) ;
2016-10-03 23:49:28 +00:00
m_threadedBasebandSampleSinks . push_back ( threadedSink ) ;
2017-02-14 20:46:35 +00:00
// initialize sample rate and center frequency in the sink:
DSPSignalNotification msg ( m_sampleRate , m_centerFrequency ) ;
threadedSink - > handleSinkMessage ( msg ) ;
// start the sink:
2015-10-15 02:05:19 +00:00
threadedSink - > start ( ) ;
}
else if ( DSPRemoveThreadedSampleSink : : match ( * message ) )
{
2016-10-03 13:55:16 +00:00
ThreadedBasebandSampleSink * threadedSink = ( ( DSPRemoveThreadedSampleSink * ) message ) - > getThreadedSampleSink ( ) ;
2015-10-15 02:05:19 +00:00
threadedSink - > stop ( ) ;
2016-10-03 23:49:28 +00:00
m_threadedBasebandSampleSinks . remove ( threadedSink ) ;
2015-10-15 02:05:19 +00:00
}
m_syncMessenger . done ( m_state ) ;
}
2016-10-03 16:29:05 +00:00
void DSPDeviceSourceEngine : : handleInputMessages ( )
2015-10-15 02:05:19 +00:00
{
2016-10-08 08:35:07 +00:00
qDebug ( ) < < " DSPDeviceSourceEngine::handleInputMessages " ;
2015-10-15 02:05:19 +00:00
Message * message ;
while ( ( message = m_inputMessageQueue . pop ( ) ) ! = 0 )
{
2016-10-08 08:35:07 +00:00
qDebug ( " DSPDeviceSourceEngine::handleInputMessages: message: %s " , message - > getIdentifier ( ) ) ;
2015-10-15 02:05:19 +00:00
if ( DSPConfigureCorrection : : match ( * message ) )
{
DSPConfigureCorrection * conf = ( DSPConfigureCorrection * ) message ;
m_iqImbalanceCorrection = conf - > getIQImbalanceCorrection ( ) ;
if ( m_dcOffsetCorrection ! = conf - > getDCOffsetCorrection ( ) )
{
m_dcOffsetCorrection = conf - > getDCOffsetCorrection ( ) ;
m_iOffset = 0 ;
m_qOffset = 0 ;
}
if ( m_iqImbalanceCorrection ! = conf - > getIQImbalanceCorrection ( ) )
{
m_iqImbalanceCorrection = conf - > getIQImbalanceCorrection ( ) ;
m_iRange = 1 < < 16 ;
m_qRange = 1 < < 16 ;
m_imbalance = 65536 ;
}
delete message ;
}
else if ( DSPSignalNotification : : match ( * message ) )
{
DSPSignalNotification * notif = ( DSPSignalNotification * ) message ;
// update DSP values
m_sampleRate = notif - > getSampleRate ( ) ;
m_centerFrequency = notif - > getCenterFrequency ( ) ;
2016-10-08 08:35:07 +00:00
qDebug ( ) < < " DSPDeviceSourceEngine::handleInputMessages: DSPSignalNotification( " < < m_sampleRate < < " , " < < m_centerFrequency < < " ) " ;
2015-10-15 02:05:19 +00:00
2017-09-16 22:06:09 +00:00
// forward source changes to channel sinks with immediate execution (no queuing)
2015-10-15 02:05:19 +00:00
2016-10-03 23:49:28 +00:00
for ( BasebandSampleSinks : : const_iterator it = m_basebandSampleSinks . begin ( ) ; it ! = m_basebandSampleSinks . end ( ) ; it + + )
2015-10-15 02:05:19 +00:00
{
2016-10-08 08:35:07 +00:00
qDebug ( ) < < " DSPDeviceSourceEngine::handleInputMessages: forward message to " < < ( * it ) - > objectName ( ) . toStdString ( ) . c_str ( ) ;
2015-10-15 02:05:19 +00:00
( * it ) - > handleMessage ( * message ) ;
}
2016-10-03 23:49:28 +00:00
for ( ThreadedBasebandSampleSinks : : const_iterator it = m_threadedBasebandSampleSinks . begin ( ) ; it ! = m_threadedBasebandSampleSinks . end ( ) ; + + it )
2015-10-15 02:05:19 +00:00
{
2016-10-08 08:35:07 +00:00
qDebug ( ) < < " DSPDeviceSourceEngine::handleSourceMessages: forward message to ThreadedSampleSink( " < < ( * it ) - > getSampleSinkObjectName ( ) . toStdString ( ) . c_str ( ) < < " ) " ;
2015-10-15 02:05:19 +00:00
( * it ) - > handleSinkMessage ( * message ) ;
}
2017-09-16 22:06:09 +00:00
// forward changes to source GUI input queue
2015-10-15 02:05:19 +00:00
2017-09-16 22:06:09 +00:00
MessageQueue * guiMessageQueue = m_deviceSampleSource - > getMessageQueueToGUI ( ) ;
if ( guiMessageQueue ) {
DSPSignalNotification * rep = new DSPSignalNotification ( * notif ) ; // make a copy for the source GUI
m_deviceSampleSource - > getMessageQueueToGUI ( ) - > push ( rep ) ;
}
//m_outputMessageQueue.push(rep);
2015-10-15 02:05:19 +00:00
delete message ;
}
}
}