| 
									
										
										
										
											2014-05-18 16:52:39 +01: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/>.          //
 | 
					
						
							|  |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-26 02:03:20 +02:00
										 |  |  | #include <QDebug>
 | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | #include <string.h>
 | 
					
						
							|  |  |  | #include <errno.h>
 | 
					
						
							|  |  |  | #include "rtlsdrinput.h"
 | 
					
						
							| 
									
										
										
										
											2016-10-11 01:17:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-22 21:47:59 +02:00
										 |  |  | #include "device/devicesourceapi.h"
 | 
					
						
							| 
									
										
										
										
											2016-10-11 01:17:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | #include "rtlsdrthread.h"
 | 
					
						
							|  |  |  | #include "rtlsdrgui.h"
 | 
					
						
							| 
									
										
										
										
											2015-08-19 22:12:08 +02:00
										 |  |  | #include "dsp/dspcommands.h"
 | 
					
						
							| 
									
										
										
										
											2015-09-27 12:50:38 +02:00
										 |  |  | #include "dsp/dspengine.h"
 | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | MESSAGE_CLASS_DEFINITION(RTLSDRInput::MsgConfigureRTLSDR, Message) | 
					
						
							|  |  |  | MESSAGE_CLASS_DEFINITION(RTLSDRInput::MsgReportRTLSDR, Message) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-11 01:17:55 +02:00
										 |  |  | RTLSDRInput::RTLSDRInput(DeviceSourceAPI *deviceAPI) : | 
					
						
							| 
									
										
										
										
											2016-05-16 02:14:36 +02:00
										 |  |  |     m_deviceAPI(deviceAPI), | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 	m_settings(), | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 	m_dev(0), | 
					
						
							|  |  |  | 	m_rtlSDRThread(0), | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 	m_deviceDescription() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | RTLSDRInput::~RTLSDRInput() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 	stop(); | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-26 02:03:20 +02:00
										 |  |  | bool RTLSDRInput::init(const Message& message) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | bool RTLSDRInput::start(int device) | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | { | 
					
						
							|  |  |  | 	QMutexLocker mutexLocker(&m_mutex); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 	if (m_dev != 0) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		stop(); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	char vendor[256]; | 
					
						
							|  |  |  | 	char product[256]; | 
					
						
							|  |  |  | 	char serial[256]; | 
					
						
							|  |  |  | 	int res; | 
					
						
							|  |  |  | 	int numberOfGains; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 	if (!m_sampleFifo.setSize(96000 * 4)) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2015-08-26 02:03:20 +02:00
										 |  |  | 		qCritical("RTLSDRInput::start: Could not allocate SampleFifo"); | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 	if ((res = rtlsdr_open(&m_dev, device)) < 0) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2015-08-26 02:03:20 +02:00
										 |  |  | 		qCritical("RTLSDRInput::start: could not open RTLSDR #%d: %s", device, strerror(errno)); | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	vendor[0] = '\0'; | 
					
						
							|  |  |  | 	product[0] = '\0'; | 
					
						
							|  |  |  | 	serial[0] = '\0'; | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if ((res = rtlsdr_get_usb_strings(m_dev, vendor, product, serial)) < 0) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2015-08-26 02:03:20 +02:00
										 |  |  | 		qCritical("RTLSDRInput::start: error accessing USB device"); | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 		stop(); | 
					
						
							|  |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-26 02:03:20 +02:00
										 |  |  | 	qWarning("RTLSDRInput::start: open: %s %s, SN: %s", vendor, product, serial); | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 	m_deviceDescription = QString("%1 (SN %2)").arg(product).arg(serial); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-26 02:03:20 +02:00
										 |  |  | 	if ((res = rtlsdr_set_sample_rate(m_dev, 1152000)) < 0) | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2015-08-26 02:03:20 +02:00
										 |  |  | 		qCritical("RTLSDRInput::start: could not set sample rate: 1024k S/s"); | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 		stop(); | 
					
						
							|  |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 	if ((res = rtlsdr_set_tuner_gain_mode(m_dev, 1)) < 0) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2015-08-26 02:03:20 +02:00
										 |  |  | 		qCritical("RTLSDRInput::start: error setting tuner gain mode"); | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 		stop(); | 
					
						
							|  |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if ((res = rtlsdr_set_agc_mode(m_dev, 0)) < 0) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2015-08-26 02:03:20 +02:00
										 |  |  | 		qCritical("RTLSDRInput::start: error setting agc mode"); | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 		stop(); | 
					
						
							|  |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	numberOfGains = rtlsdr_get_tuner_gains(m_dev, NULL); | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (numberOfGains < 0) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2015-08-26 02:03:20 +02:00
										 |  |  | 		qCritical("RTLSDRInput::start: error getting number of gain values supported"); | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 		stop(); | 
					
						
							|  |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 	m_gains.resize(numberOfGains); | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (rtlsdr_get_tuner_gains(m_dev, &m_gains[0]) < 0) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2015-08-26 02:03:20 +02:00
										 |  |  | 		qCritical("RTLSDRInput::start: error getting gain values"); | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 		stop(); | 
					
						
							|  |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-08-26 02:03:20 +02:00
										 |  |  | 	else | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		qDebug() << "RTLSDRInput::start: " << m_gains.size() << "gains"; | 
					
						
							|  |  |  | 		MsgReportRTLSDR *message = MsgReportRTLSDR::create(m_gains); | 
					
						
							|  |  |  | 		getOutputMessageQueueToGUI()->push(message); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if ((res = rtlsdr_reset_buffer(m_dev)) < 0) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2015-08-26 02:03:20 +02:00
										 |  |  | 		qCritical("RTLSDRInput::start: could not reset USB EP buffers: %s", strerror(errno)); | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 		stop(); | 
					
						
							|  |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 	if ((m_rtlSDRThread = new RTLSDRThread(m_dev, &m_sampleFifo)) == NULL) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2015-08-26 02:03:20 +02:00
										 |  |  | 		qFatal("RTLSDRInput::start: out of memory"); | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 		stop(); | 
					
						
							|  |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 	m_rtlSDRThread->startWork(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	mutexLocker.unlock(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 	applySettings(m_settings, true); | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 	return true; | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | void RTLSDRInput::stop() | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | { | 
					
						
							|  |  |  | 	QMutexLocker mutexLocker(&m_mutex); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 	if (m_rtlSDRThread != 0) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 		m_rtlSDRThread->stopWork(); | 
					
						
							|  |  |  | 		delete m_rtlSDRThread; | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 		m_rtlSDRThread = 0; | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (m_dev != 0) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 		rtlsdr_close(m_dev); | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 		m_dev = 0; | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 	m_deviceDescription.clear(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const QString& RTLSDRInput::getDeviceDescription() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return m_deviceDescription; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int RTLSDRInput::getSampleRate() const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 	int rate = m_settings.m_devSampleRate; | 
					
						
							| 
									
										
										
										
											2015-05-09 16:44:26 +02:00
										 |  |  | 	return (rate / (1<<m_settings.m_log2Decim)); | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | quint64 RTLSDRInput::getCenterFrequency() const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 	return m_settings.m_centerFrequency; | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | bool RTLSDRInput::handleMessage(const Message& message) | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 	if (MsgConfigureRTLSDR::match(message)) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		MsgConfigureRTLSDR& conf = (MsgConfigureRTLSDR&) message; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (!applySettings(conf.getSettings(), false)) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 			qDebug("RTLSDR config error"); | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 		return true; | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-01 04:39:00 +02:00
										 |  |  | bool RTLSDRInput::applySettings(const RTLSDRSettings& settings, bool force) | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | { | 
					
						
							|  |  |  | 	QMutexLocker mutexLocker(&m_mutex); | 
					
						
							| 
									
										
										
										
											2015-08-19 22:12:08 +02:00
										 |  |  |     bool forwardChange = false; | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 	if ((m_settings.m_gain != settings.m_gain) || force) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 		m_settings.m_gain = settings.m_gain; | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if(m_dev != 0) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 			if(rtlsdr_set_tuner_gain(m_dev, m_settings.m_gain) != 0) | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 				qDebug("rtlsdr_set_tuner_gain() failed"); | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-11-08 23:42:43 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 	if ((m_settings.m_devSampleRate != settings.m_devSampleRate) || force) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2015-08-19 22:12:08 +02:00
										 |  |  |         forwardChange = true; | 
					
						
							| 
									
										
										
										
											2016-05-12 23:35:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 		if(m_dev != 0) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			if( rtlsdr_set_sample_rate(m_dev, settings.m_devSampleRate) < 0) | 
					
						
							|  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2015-10-01 06:30:50 +02:00
										 |  |  | 				qCritical("RTLSDRInput::applySettings: could not set sample rate: %d", settings.m_devSampleRate); | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			else | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				m_settings.m_devSampleRate = settings.m_devSampleRate; | 
					
						
							|  |  |  | 				m_rtlSDRThread->setSamplerate(settings.m_devSampleRate); | 
					
						
							| 
									
										
										
										
											2015-10-01 06:30:50 +02:00
										 |  |  | 				qDebug("RTLSDRInput::applySettings: sample rate set to %d", m_settings.m_devSampleRate); | 
					
						
							| 
									
										
										
										
											2014-11-08 23:42:43 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 	if ((m_settings.m_loPpmCorrection != settings.m_loPpmCorrection) || force) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if (m_dev != 0) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			if (rtlsdr_set_freq_correction(m_dev, settings.m_loPpmCorrection) < 0) | 
					
						
							|  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2015-05-09 00:54:01 +02:00
										 |  |  | 				qCritical("could not set LO ppm correction: %d", settings.m_loPpmCorrection); | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			else | 
					
						
							|  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2015-05-09 00:54:01 +02:00
										 |  |  | 				m_settings.m_loPpmCorrection = settings.m_loPpmCorrection; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 	if ((m_settings.m_log2Decim != settings.m_log2Decim) || force) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2015-08-19 22:12:08 +02:00
										 |  |  |         forwardChange = true; | 
					
						
							| 
									
										
										
										
											2016-05-12 23:35:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 		if(m_dev != 0) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2015-05-09 16:44:26 +02:00
										 |  |  | 			m_settings.m_log2Decim = settings.m_log2Decim; | 
					
						
							|  |  |  | 			m_rtlSDRThread->setLog2Decimation(settings.m_log2Decim); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-19 22:12:08 +02:00
										 |  |  |     if (m_settings.m_centerFrequency != settings.m_centerFrequency) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         forwardChange = true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-01 06:30:50 +02:00
										 |  |  | 	qint64 deviceCenterFrequency = m_settings.m_centerFrequency; | 
					
						
							|  |  |  | 	qint64 f_img = deviceCenterFrequency; | 
					
						
							|  |  |  | 	quint32 devSampleRate = m_settings.m_devSampleRate; | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-01 06:30:50 +02:00
										 |  |  | 	if (force || (m_settings.m_centerFrequency != settings.m_centerFrequency) | 
					
						
							|  |  |  | 			|| (m_settings.m_fcPos != settings.m_fcPos)) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		m_settings.m_centerFrequency = settings.m_centerFrequency; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if ((m_settings.m_log2Decim == 0) || (settings.m_fcPos == RTLSDRSettings::FC_POS_CENTER)) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			deviceCenterFrequency = m_settings.m_centerFrequency; | 
					
						
							|  |  |  | 			f_img = deviceCenterFrequency; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			if (settings.m_fcPos == RTLSDRSettings::FC_POS_INFRA) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				deviceCenterFrequency = m_settings.m_centerFrequency + (devSampleRate / 4); | 
					
						
							|  |  |  | 				f_img = deviceCenterFrequency + devSampleRate/2; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			else if (settings.m_fcPos == RTLSDRSettings::FC_POS_SUPRA) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				deviceCenterFrequency = m_settings.m_centerFrequency - (devSampleRate / 4); | 
					
						
							|  |  |  | 				f_img = deviceCenterFrequency - devSampleRate/2; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if(m_dev != 0) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			if (rtlsdr_set_center_freq( m_dev, deviceCenterFrequency ) != 0) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				qDebug("rtlsdr_set_center_freq(%lld) failed", deviceCenterFrequency); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			else | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				qDebug() << "RTLSDRInput::applySettings: center freq: " << m_settings.m_centerFrequency << " Hz" | 
					
						
							|  |  |  | 						<< " device center freq: " << deviceCenterFrequency << " Hz" | 
					
						
							|  |  |  | 						<< " device sample rate: " << devSampleRate << "Hz" | 
					
						
							|  |  |  | 						<< " Actual sample rate: " << devSampleRate/(1<<m_settings.m_log2Decim) << "Hz" | 
					
						
							|  |  |  | 						<< " img: " << f_img << "Hz"; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if ((m_settings.m_fcPos != settings.m_fcPos) || force) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		m_settings.m_fcPos = settings.m_fcPos; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if(m_dev != 0) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			m_rtlSDRThread->setFcPos((int) m_settings.m_fcPos); | 
					
						
							|  |  |  | 			qDebug() << "RTLSDRInput: set fc pos (enum) to " << (int) m_settings.m_fcPos; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/*
 | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 	if(m_dev != 0) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		qint64 centerFrequency = m_settings.m_centerFrequency + (m_settings.m_devSampleRate / 4); | 
					
						
							| 
									
										
										
										
											2015-05-09 19:03:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 		if (m_settings.m_log2Decim == 0) | 
					
						
							|  |  |  | 		{ // Little wooby-doop if no decimation
 | 
					
						
							|  |  |  | 			centerFrequency = m_settings.m_centerFrequency; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			centerFrequency = m_settings.m_centerFrequency + (m_settings.m_devSampleRate / 4); | 
					
						
							| 
									
										
										
										
											2015-05-09 19:03:17 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-17 08:29:34 +02:00
										 |  |  | 		if (rtlsdr_set_center_freq( m_dev, centerFrequency ) != 0) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			qDebug("rtlsdr_set_center_freq(%lld) failed", m_settings.m_centerFrequency); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-10-01 06:30:50 +02:00
										 |  |  | 	}*/ | 
					
						
							| 
									
										
										
										
											2016-05-12 23:35:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-01 05:05:46 +02:00
										 |  |  | 	if ((m_settings.m_dcBlock != settings.m_dcBlock) || force) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		m_settings.m_dcBlock = settings.m_dcBlock; | 
					
						
							| 
									
										
										
										
											2016-05-16 02:14:36 +02:00
										 |  |  | 		m_deviceAPI->configureCorrections(m_settings.m_dcBlock, m_settings.m_iqImbalance); | 
					
						
							| 
									
										
										
										
											2015-10-01 05:05:46 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if ((m_settings.m_iqImbalance != settings.m_iqImbalance) || force) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		m_settings.m_iqImbalance = settings.m_iqImbalance; | 
					
						
							| 
									
										
										
										
											2016-05-16 02:14:36 +02:00
										 |  |  | 		m_deviceAPI->configureCorrections(m_settings.m_dcBlock, m_settings.m_iqImbalance); | 
					
						
							| 
									
										
										
										
											2015-10-01 05:05:46 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-19 22:12:08 +02:00
										 |  |  |     if (forwardChange) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  | 		int sampleRate = m_settings.m_devSampleRate/(1<<m_settings.m_log2Decim); | 
					
						
							|  |  |  | 		DSPSignalNotification *notif = new DSPSignalNotification(sampleRate, m_settings.m_centerFrequency); | 
					
						
							| 
									
										
										
										
											2016-05-16 02:14:36 +02:00
										 |  |  | 		m_deviceAPI->getDeviceInputMessageQueue()->push(notif); | 
					
						
							| 
									
										
										
										
											2015-08-19 22:12:08 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-11-08 23:42:43 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-18 16:52:39 +01:00
										 |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-11-07 19:26:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-08 23:42:43 +00:00
										 |  |  | void RTLSDRInput::set_ds_mode(int on) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-11-07 19:26:02 +00:00
										 |  |  | 	rtlsdr_set_direct_sampling(m_dev, on); | 
					
						
							| 
									
										
										
										
											2014-11-08 23:42:43 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 |