| 
									
										
										
										
											2014-11-03 17:13:32 +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 "v4linput.h"
 | 
					
						
							|  |  |  | #include "v4lthread.h"
 | 
					
						
							|  |  |  | #include "v4lgui.h"
 | 
					
						
							|  |  |  | #include "util/simpleserializer.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | MESSAGE_CLASS_DEFINITION(V4LInput::MsgConfigureV4L, Message) | 
					
						
							|  |  |  | MESSAGE_CLASS_DEFINITION(V4LInput::MsgReportV4L, Message) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | V4LInput::Settings::Settings() : | 
					
						
							| 
									
										
										
										
											2014-11-12 17:37:46 +00:00
										 |  |  | 	m_gain(0) | 
					
						
							| 
									
										
										
										
											2014-11-03 17:13:32 +00:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void V4LInput::Settings::resetToDefaults() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	m_gain = 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | QByteArray V4LInput::Settings::serialize() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	SimpleSerializer s(1); | 
					
						
							|  |  |  | 	s.writeS32(1, m_gain); | 
					
						
							| 
									
										
										
										
											2014-11-12 18:22:49 +00:00
										 |  |  | 	s.writeS32(2, SAMPLERATE); | 
					
						
							| 
									
										
										
										
											2014-11-03 17:13:32 +00:00
										 |  |  | 	return s.final(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool V4LInput::Settings::deserialize(const QByteArray& data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	SimpleDeserializer d(data); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if(!d.isValid()) { | 
					
						
							|  |  |  | 		resetToDefaults(); | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if(d.getVersion() == 1) { | 
					
						
							|  |  |  | 		d.readS32(1, &m_gain, 0); | 
					
						
							| 
									
										
										
										
											2014-11-12 18:22:49 +00:00
										 |  |  | 		//d.readS32(2, &m_samplerate, 0);
 | 
					
						
							| 
									
										
										
										
											2014-11-03 17:13:32 +00:00
										 |  |  | 		return true; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		resetToDefaults(); | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | V4LInput::V4LInput(MessageQueue* msgQueueToGUI) : | 
					
						
							|  |  |  | 	SampleSource(msgQueueToGUI), | 
					
						
							|  |  |  | 	m_settings(), | 
					
						
							|  |  |  | 	m_V4LThread(NULL), | 
					
						
							|  |  |  | 	m_deviceDescription() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | V4LInput::~V4LInput() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	stopInput(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool V4LInput::startInput(int device) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	QMutexLocker mutexLocker(&m_mutex); | 
					
						
							| 
									
										
										
										
											2014-12-03 00:13:18 +00:00
										 |  |  | 	double freq; | 
					
						
							| 
									
										
										
										
											2014-11-03 17:13:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-12 18:22:49 +00:00
										 |  |  | 	if(m_V4LThread) | 
					
						
							|  |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2014-11-05 21:19:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-12 17:37:46 +00:00
										 |  |  | 	if(!m_sampleFifo.setSize(4096*16)) { | 
					
						
							| 
									
										
										
										
											2014-11-03 17:13:32 +00:00
										 |  |  | 		qCritical("Could not allocate SampleFifo"); | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-03 00:13:18 +00:00
										 |  |  | 	freq = (double)getCenterFrequency(); | 
					
						
							|  |  |  | 	if((m_V4LThread = new V4LThread(&m_sampleFifo, freq)) == NULL) { | 
					
						
							| 
									
										
										
										
											2014-11-03 17:13:32 +00:00
										 |  |  | 		qFatal("out of memory"); | 
					
						
							| 
									
										
										
										
											2014-11-12 18:22:49 +00:00
										 |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2014-11-03 17:13:32 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-12 18:22:49 +00:00
										 |  |  | 	m_deviceDescription = QString("RTL-SDR /dev/swradio0"); | 
					
						
							| 
									
										
										
										
											2014-11-03 17:13:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-05 21:19:25 +00:00
										 |  |  | 	qDebug("V4LInput: start"); | 
					
						
							| 
									
										
										
										
											2014-12-03 00:13:18 +00:00
										 |  |  | 	//MsgReportV4L::create(m_gains)->submit(m_guiMessageQueue);
 | 
					
						
							| 
									
										
										
										
											2014-11-03 17:13:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void V4LInput::stopInput() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	QMutexLocker mutexLocker(&m_mutex); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-12 18:22:49 +00:00
										 |  |  | 	if(m_V4LThread) { | 
					
						
							| 
									
										
										
										
											2014-11-03 17:13:32 +00:00
										 |  |  | 		m_V4LThread->stopWork(); | 
					
						
							| 
									
										
										
										
											2014-11-12 18:22:49 +00:00
										 |  |  | 		// wait for thread to quit ?
 | 
					
						
							| 
									
										
										
										
											2014-11-03 17:13:32 +00:00
										 |  |  | 		delete m_V4LThread; | 
					
						
							|  |  |  | 		m_V4LThread = NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	m_deviceDescription.clear(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const QString& V4LInput::getDeviceDescription() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return m_deviceDescription; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int V4LInput::getSampleRate() const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-11-12 18:22:49 +00:00
										 |  |  | 	int result = SAMPLERATE / 4; | 
					
						
							| 
									
										
										
										
											2014-11-06 21:03:17 +00:00
										 |  |  | 	return result; | 
					
						
							| 
									
										
										
										
											2014-11-03 17:13:32 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | quint64 V4LInput::getCenterFrequency() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return m_generalSettings.m_centerFrequency; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool V4LInput::handleMessage(Message* message) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if(MsgConfigureV4L::match(message)) { | 
					
						
							|  |  |  | 		MsgConfigureV4L* conf = (MsgConfigureV4L*)message; | 
					
						
							| 
									
										
										
										
											2014-12-03 00:13:18 +00:00
										 |  |  | 		applySettings(conf->getGeneralSettings(), conf->getSettings(), false); | 
					
						
							| 
									
										
										
										
											2014-11-03 17:13:32 +00:00
										 |  |  | 		message->completed(); | 
					
						
							|  |  |  | 		return true; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-03 00:13:18 +00:00
										 |  |  | void V4LInput::applySettings(const GeneralSettings& generalSettings, const Settings& settings, bool force) | 
					
						
							| 
									
										
										
										
											2014-11-03 17:13:32 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	QMutexLocker mutexLocker(&m_mutex); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-03 00:13:18 +00:00
										 |  |  | 	if (!m_V4LThread) { | 
					
						
							|  |  |  | 			m_generalSettings.m_centerFrequency = generalSettings.m_centerFrequency; | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-03 17:13:32 +00:00
										 |  |  | 	if((m_generalSettings.m_centerFrequency != generalSettings.m_centerFrequency) || force) { | 
					
						
							| 
									
										
										
										
											2014-12-03 00:13:18 +00:00
										 |  |  | 		m_V4LThread->set_center_freq( (double)(generalSettings.m_centerFrequency | 
					
						
							| 
									
										
										
										
											2014-11-12 18:22:49 +00:00
										 |  |  | 								+ (SAMPLERATE / 4) )); | 
					
						
							| 
									
										
										
										
											2014-11-03 17:13:32 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-12-03 00:13:18 +00:00
										 |  |  | 	m_generalSettings.m_centerFrequency = generalSettings.m_centerFrequency; | 
					
						
							|  |  |  | #if 0
 | 
					
						
							| 
									
										
										
										
											2014-11-03 17:13:32 +00:00
										 |  |  | 	if((m_settings.m_gain != settings.m_gain) || force) { | 
					
						
							|  |  |  | 		m_settings.m_gain = settings.m_gain; | 
					
						
							| 
									
										
										
										
											2014-12-03 00:13:18 +00:00
										 |  |  | 		m_V4LThread->set_tuner_gain((double)m_settings.m_gain); | 
					
						
							| 
									
										
										
										
											2014-11-03 17:13:32 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-12-03 00:13:18 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2014-11-03 17:13:32 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-12-03 00:13:18 +00:00
										 |  |  | 
 |