| 
									
										
										
										
											2016-10-17 18:15:08 +02:00
										 |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | // Copyright (C) 2016 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/>.          //
 | 
					
						
							|  |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifndef SDRBASE_DSP_DSPDEVICESINKENGINE_H_
 | 
					
						
							|  |  |  | #define SDRBASE_DSP_DSPDEVICESINKENGINE_H_
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <QThread>
 | 
					
						
							|  |  |  | #include <QTimer>
 | 
					
						
							|  |  |  | #include <QMutex>
 | 
					
						
							|  |  |  | #include <QWaitCondition>
 | 
					
						
							| 
									
										
										
										
											2016-12-26 12:45:19 +01:00
										 |  |  | #include <stdint.h>
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:15:08 +02:00
										 |  |  | #include <list>
 | 
					
						
							| 
									
										
										
										
											2016-12-26 12:11:51 +01:00
										 |  |  | #include <map>
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:15:08 +02:00
										 |  |  | #include "dsp/dsptypes.h"
 | 
					
						
							|  |  |  | #include "dsp/fftwindow.h"
 | 
					
						
							|  |  |  | #include "util/messagequeue.h"
 | 
					
						
							|  |  |  | #include "util/syncmessenger.h"
 | 
					
						
							|  |  |  | #include "util/export.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class DeviceSampleSink; | 
					
						
							|  |  |  | class BasebandSampleSource; | 
					
						
							|  |  |  | class ThreadedBasebandSampleSource; | 
					
						
							|  |  |  | class BasebandSampleSink; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class SDRANGEL_API DSPDeviceSinkEngine : public QThread { | 
					
						
							|  |  |  | 	Q_OBJECT | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  | 	enum State { | 
					
						
							|  |  |  | 		StNotStarted,  //!< engine is before initialization
 | 
					
						
							|  |  |  | 		StIdle,        //!< engine is idle
 | 
					
						
							|  |  |  | 		StReady,       //!< engine is ready to run
 | 
					
						
							|  |  |  | 		StRunning,     //!< engine is running
 | 
					
						
							|  |  |  | 		StError        //!< engine is in error
 | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-26 12:45:19 +01:00
										 |  |  | 	DSPDeviceSinkEngine(uint32_t uid, QObject* parent = NULL); | 
					
						
							| 
									
										
										
										
											2016-10-17 18:15:08 +02:00
										 |  |  | 	~DSPDeviceSinkEngine(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-26 12:45:19 +01:00
										 |  |  | 	uint32_t getUID() const { return m_uid; } | 
					
						
							| 
									
										
										
										
											2016-10-17 18:15:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	MessageQueue* getInputMessageQueue() { return &m_inputMessageQueue; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void start(); //!< This thread start
 | 
					
						
							|  |  |  | 	void stop();  //!< This thread stop
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bool initGeneration(); //!< Initialize generation sequence
 | 
					
						
							|  |  |  | 	bool startGeneration(); //!< Start generation sequence
 | 
					
						
							|  |  |  | 	void stopGeneration();   //!< Stop generation sequence
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void setSink(DeviceSampleSink* sink); //!< Set the sample sink type
 | 
					
						
							| 
									
										
										
										
											2017-09-16 11:34:25 +02:00
										 |  |  | 	DeviceSampleSink *getSink() { return m_deviceSampleSink; } | 
					
						
							| 
									
										
										
										
											2016-10-17 18:15:08 +02:00
										 |  |  | 	void setSinkSequence(int sequence); //!< Set the sample sink sequence in type
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void addSource(BasebandSampleSource* source); //!< Add a baseband sample source
 | 
					
						
							|  |  |  | 	void removeSource(BasebandSampleSource* source); //!< Remove a baseband sample source
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void addThreadedSource(ThreadedBasebandSampleSource* source); //!< Add a baseband sample source that will run on its own thread
 | 
					
						
							|  |  |  | 	void removeThreadedSource(ThreadedBasebandSampleSource* source); //!< Remove a baseband sample source that runs on its own thread
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-26 15:26:25 +01:00
										 |  |  | 	uint32_t getNumberOfSources() const { return m_basebandSampleSources.size() + m_threadedBasebandSampleSources.size(); } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-24 18:06:44 +02:00
										 |  |  | 	void addSpectrumSink(BasebandSampleSink* spectrumSink);    //!< Add a spectrum vis baseband sample sink
 | 
					
						
							|  |  |  | 	void removeSpectrumSink(BasebandSampleSink* spectrumSink); //!< Add a spectrum vis baseband sample sink
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:15:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	State state() const { return m_state; } //!< Return DSP engine current state
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	QString errorMessage(); //!< Return the current error message
 | 
					
						
							|  |  |  | 	QString sinkDeviceDescription(); //!< Return the sink device description
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2016-12-26 12:45:19 +01:00
										 |  |  | 	uint32_t m_uid; //!< unique ID
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:15:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	MessageQueue m_inputMessageQueue;  //<! Input message queue. Post here.
 | 
					
						
							|  |  |  | 	SyncMessenger m_syncMessenger;     //!< Used to process messages synchronously with the thread
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	State m_state; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	QString m_errorMessage; | 
					
						
							|  |  |  | 	QString m_deviceDescription; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	DeviceSampleSink* m_deviceSampleSink; | 
					
						
							|  |  |  | 	int m_sampleSinkSequence; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	typedef std::list<BasebandSampleSource*> BasebandSampleSources; | 
					
						
							|  |  |  | 	BasebandSampleSources m_basebandSampleSources; //!< baseband sample sources within main thread (usually file input)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	typedef std::list<ThreadedBasebandSampleSource*> ThreadedBasebandSampleSources; | 
					
						
							|  |  |  | 	ThreadedBasebandSampleSources m_threadedBasebandSampleSources; //!< baseband sample sources on their own threads (usually channels)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-26 12:11:51 +01:00
										 |  |  | 	typedef std::map<BasebandSampleSource*, SampleVector::iterator> BasebandSampleSourcesIteratorMap; | 
					
						
							|  |  |  | 	typedef std::pair<BasebandSampleSource*, SampleVector::iterator> BasebandSampleSourcesIteratorMapKV; | 
					
						
							|  |  |  | 	BasebandSampleSourcesIteratorMap m_basebandSampleSourcesIteratorMap; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     typedef std::map<ThreadedBasebandSampleSource*, SampleVector::iterator> ThreadedBasebandSampleSourcesIteratorMap; | 
					
						
							|  |  |  |     typedef std::pair<ThreadedBasebandSampleSource*, SampleVector::iterator> ThreadedBasebandSampleSourcesIteratorMapKV; | 
					
						
							|  |  |  |     ThreadedBasebandSampleSourcesIteratorMap m_threadedBasebandSampleSourcesIteratorMap; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-24 18:06:44 +02:00
										 |  |  | 	BasebandSampleSink *m_spectrumSink; | 
					
						
							| 
									
										
										
										
											2016-10-17 18:15:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-26 12:45:19 +01:00
										 |  |  | 	uint32_t m_sampleRate; | 
					
						
							| 
									
										
										
										
											2016-10-17 18:15:08 +02:00
										 |  |  | 	quint64 m_centerFrequency; | 
					
						
							| 
									
										
										
										
											2016-12-26 12:45:19 +01:00
										 |  |  | 	uint32_t m_multipleSourcesDivisionFactor; | 
					
						
							| 
									
										
										
										
											2016-10-17 18:15:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	void run(); | 
					
						
							| 
									
										
										
										
											2016-12-21 02:24:49 +01:00
										 |  |  | 	void work(int nbWriteSamples); //!< transfer samples from beseband sources to sink if in running state
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:15:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	State gotoIdle();     //!< Go to the idle state
 | 
					
						
							|  |  |  | 	State gotoInit();     //!< Go to the acquisition init state from idle
 | 
					
						
							|  |  |  | 	State gotoRunning();  //!< Go to the running state from ready state
 | 
					
						
							|  |  |  | 	State gotoError(const QString& errorMsg); //!< Go to an error state
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void handleSetSink(DeviceSampleSink* sink); //!< Manage sink setting
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private slots: | 
					
						
							| 
									
										
										
										
											2016-12-21 02:24:49 +01:00
										 |  |  | 	void handleData(int nbSamples); //!< Handle data when samples have to be written to the sample FIFO
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:15:08 +02:00
										 |  |  | 	void handleInputMessages(); //!< Handle input message queue
 | 
					
						
							|  |  |  | 	void handleSynchronousMessages(); //!< Handle synchronous messages with the thread
 | 
					
						
							| 
									
										
										
										
											2016-10-24 18:31:14 +02:00
										 |  |  | 	void handleForwardToSpectrumSink(int nbSamples); | 
					
						
							| 
									
										
										
										
											2016-10-17 18:15:08 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif /* SDRBASE_DSP_DSPDEVICESINKENGINE_H_ */
 |