| 
									
										
										
										
											2014-11-29 18:09:35 +00:00
										 |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | // Copyright (C) 2012 maintech GmbH, Otto-Hahn-Str. 15, 97204 Hoechberg, Germany //
 | 
					
						
							|  |  |  | // written by Christian Daniel                                                   //
 | 
					
						
							|  |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // 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/>.          //
 | 
					
						
							|  |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | #include <errno.h>
 | 
					
						
							|  |  |  | #include "fcdinput.h"
 | 
					
						
							|  |  |  | #include "fcdthread.h"
 | 
					
						
							|  |  |  | #include "fcdgui.h"
 | 
					
						
							|  |  |  | #include "util/simpleserializer.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | MESSAGE_CLASS_DEFINITION(FCDInput::MsgConfigureFCD, Message) | 
					
						
							|  |  |  | //MESSAGE_CLASS_DEFINITION(FCDInput::MsgReportFCD, Message)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-02 14:23:21 +00:00
										 |  |  | FCDInput::Settings::Settings() : | 
					
						
							|  |  |  | 	range(0), | 
					
						
							|  |  |  | 	gain(0), | 
					
						
							|  |  |  | 	bias(0) | 
					
						
							| 
									
										
										
										
											2014-11-29 18:09:35 +00:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void FCDInput::Settings::resetToDefaults() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-12-02 14:23:21 +00:00
										 |  |  | 	range = 0; | 
					
						
							|  |  |  | 	gain = 0; | 
					
						
							|  |  |  | 	bias = 0; | 
					
						
							| 
									
										
										
										
											2014-11-29 18:09:35 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | QByteArray FCDInput::Settings::serialize() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	SimpleSerializer s(1); | 
					
						
							| 
									
										
										
										
											2014-12-02 14:23:21 +00:00
										 |  |  | 	s.writeS32(1, range); | 
					
						
							|  |  |  | 	s.writeS32(2, gain); | 
					
						
							|  |  |  | 	s.writeS32(3, bias); | 
					
						
							| 
									
										
										
										
											2014-11-29 18:09:35 +00:00
										 |  |  | 	return s.final(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool FCDInput::Settings::deserialize(const QByteArray& data) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-12-02 14:23:21 +00:00
										 |  |  | 	SimpleDeserializer d(data); | 
					
						
							|  |  |  | 	if(d.isValid() && d.getVersion() == 1) { | 
					
						
							|  |  |  |                 d.readS32(1, &range, 0); | 
					
						
							|  |  |  | 		d.readS32(2, &gain, 0); | 
					
						
							|  |  |  | 		d.readS32(3, &bias, 0); | 
					
						
							|  |  |  | 		return true; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-11-29 18:09:35 +00:00
										 |  |  | 	resetToDefaults(); | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | FCDInput::FCDInput(MessageQueue* msgQueueToGUI) : | 
					
						
							|  |  |  | 	SampleSource(msgQueueToGUI), | 
					
						
							|  |  |  | 	m_settings(), | 
					
						
							|  |  |  | 	m_FCDThread(NULL), | 
					
						
							|  |  |  | 	m_deviceDescription() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | FCDInput::~FCDInput() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	stopInput(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool FCDInput::startInput(int device) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	QMutexLocker mutexLocker(&m_mutex); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if(m_FCDThread) | 
					
						
							|  |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2014-12-10 18:24:01 +00:00
										 |  |  | 	/* Apply settings before streaming to avoid bus contention;
 | 
					
						
							|  |  |  | 	 * there is very little spare bandwidth on a full speed USB device. | 
					
						
							|  |  |  | 	 * Failure is harmless if no device is found */ | 
					
						
							|  |  |  | 	applySettings(m_generalSettings, m_settings, true); | 
					
						
							| 
									
										
										
										
											2014-11-29 18:09:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if(!m_sampleFifo.setSize(4096*16)) { | 
					
						
							|  |  |  | 		qCritical("Could not allocate SampleFifo"); | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if((m_FCDThread = new FCDThread(&m_sampleFifo)) == NULL) { | 
					
						
							|  |  |  | 		qFatal("out of memory"); | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	m_deviceDescription = QString("Funcube Dongle"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	qDebug("FCDInput: start"); | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void FCDInput::stopInput() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	QMutexLocker mutexLocker(&m_mutex); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if(m_FCDThread) { | 
					
						
							|  |  |  | 		m_FCDThread->stopWork(); | 
					
						
							|  |  |  | 		// wait for thread to quit ?
 | 
					
						
							|  |  |  | 		delete m_FCDThread; | 
					
						
							|  |  |  | 		m_FCDThread = NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	m_deviceDescription.clear(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const QString& FCDInput::getDeviceDescription() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return m_deviceDescription; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int FCDInput::getSampleRate() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return 192000; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | quint64 FCDInput::getCenterFrequency() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return m_generalSettings.m_centerFrequency; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool FCDInput::handleMessage(Message* message) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if(MsgConfigureFCD::match(message)) { | 
					
						
							|  |  |  | 		MsgConfigureFCD* conf = (MsgConfigureFCD*)message; | 
					
						
							| 
									
										
										
										
											2014-12-02 19:00:12 +00:00
										 |  |  | 		applySettings(conf->getGeneralSettings(), conf->getSettings(), false); | 
					
						
							| 
									
										
										
										
											2014-11-29 18:09:35 +00:00
										 |  |  | 		message->completed(); | 
					
						
							|  |  |  | 		return true; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-02 19:00:12 +00:00
										 |  |  | void FCDInput::applySettings(const GeneralSettings& generalSettings, const Settings& settings, bool force) | 
					
						
							| 
									
										
										
										
											2014-11-29 18:09:35 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-12-02 15:39:37 +00:00
										 |  |  | 	bool freqChange; | 
					
						
							| 
									
										
										
										
											2014-11-29 18:09:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-02 15:39:37 +00:00
										 |  |  | 	if((m_generalSettings.m_centerFrequency != generalSettings.m_centerFrequency)) | 
					
						
							|  |  |  | 		freqChange = true; | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		freqChange = false; | 
					
						
							| 
									
										
										
										
											2014-12-02 14:23:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-02 15:39:37 +00:00
										 |  |  | 	if(freqChange || force) { | 
					
						
							| 
									
										
										
										
											2014-11-29 18:09:35 +00:00
										 |  |  | 		m_generalSettings.m_centerFrequency = generalSettings.m_centerFrequency; | 
					
						
							| 
									
										
										
										
											2014-12-02 19:00:12 +00:00
										 |  |  | 		set_center_freq( (double)(generalSettings.m_centerFrequency) ); | 
					
						
							| 
									
										
										
										
											2014-12-02 15:39:37 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if(!freqChange || force) { | 
					
						
							| 
									
										
										
										
											2014-12-02 19:00:12 +00:00
										 |  |  | 		set_lna_gain(settings.gain); | 
					
						
							|  |  |  | 		set_bias_t(settings.bias); | 
					
						
							| 
									
										
										
										
											2014-11-29 18:09:35 +00:00
										 |  |  | 	}	 | 
					
						
							| 
									
										
										
										
											2014-12-02 19:00:12 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-29 18:09:35 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-12-02 14:23:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-02 19:00:12 +00:00
										 |  |  | 
 |