1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2026-06-02 06:04:39 -04:00

RTL-SDR: refactoring: device open close moved in the constructor and destructor respectively of the input object

This commit is contained in:
f4exb
2017-04-13 01:21:25 +02:00
parent 934e73f7a7
commit bb5fd95f33
5 changed files with 216 additions and 139 deletions
+142 -100
View File
@@ -28,6 +28,7 @@
#include "dsp/dspengine.h"
MESSAGE_CLASS_DEFINITION(RTLSDRInput::MsgConfigureRTLSDR, Message)
MESSAGE_CLASS_DEFINITION(RTLSDRInput::MsgQueryRTLSDR, Message)
MESSAGE_CLASS_DEFINITION(RTLSDRInput::MsgReportRTLSDR, Message)
RTLSDRInput::RTLSDRInput(DeviceSourceAPI *deviceAPI) :
@@ -35,107 +36,128 @@ RTLSDRInput::RTLSDRInput(DeviceSourceAPI *deviceAPI) :
m_settings(),
m_dev(0),
m_rtlSDRThread(0),
m_deviceDescription()
m_deviceDescription(),
m_running(false)
{
openDevice();
}
RTLSDRInput::~RTLSDRInput()
{
stop();
//stop();
if (m_running) stop();
closeDevice();
}
bool RTLSDRInput::start(int device)
bool RTLSDRInput::openDevice()
{
if (m_dev != 0)
{
closeDevice();
}
char vendor[256];
char product[256];
char serial[256];
int res;
int numberOfGains;
if (!m_sampleFifo.setSize(96000 * 4))
{
qCritical("RTLSDRInput::openDevice: Could not allocate SampleFifo");
return false;
}
int device;
if ((device = rtlsdr_get_index_by_serial(qPrintable(m_deviceAPI->getSampleSourceSerial()))) < 0)
{
qCritical("RTLSDRInput::openDevice: could not get RTLSDR serial number");
return false;
}
if ((res = rtlsdr_open(&m_dev, device)) < 0)
{
qCritical("RTLSDRInput::openDevice: could not open RTLSDR #%d: %s", device, strerror(errno));
return false;
}
vendor[0] = '\0';
product[0] = '\0';
serial[0] = '\0';
if ((res = rtlsdr_get_usb_strings(m_dev, vendor, product, serial)) < 0)
{
qCritical("RTLSDRInput::openDevice: error accessing USB device");
stop();
return false;
}
qWarning("RTLSDRInput::openDevice: open: %s %s, SN: %s", vendor, product, serial);
m_deviceDescription = QString("%1 (SN %2)").arg(product).arg(serial);
if ((res = rtlsdr_set_sample_rate(m_dev, 1152000)) < 0)
{
qCritical("RTLSDRInput::openDevice: could not set sample rate: 1024k S/s");
stop();
return false;
}
if ((res = rtlsdr_set_tuner_gain_mode(m_dev, 1)) < 0)
{
qCritical("RTLSDRInput::openDevice: error setting tuner gain mode");
stop();
return false;
}
if ((res = rtlsdr_set_agc_mode(m_dev, 0)) < 0)
{
qCritical("RTLSDRInput::openDevice: error setting agc mode");
stop();
return false;
}
numberOfGains = rtlsdr_get_tuner_gains(m_dev, NULL);
if (numberOfGains < 0)
{
qCritical("RTLSDRInput::openDevice: error getting number of gain values supported");
stop();
return false;
}
m_gains.resize(numberOfGains);
if (rtlsdr_get_tuner_gains(m_dev, &m_gains[0]) < 0)
{
qCritical("RTLSDRInput::openDevice: error getting gain values");
stop();
return false;
}
else
{
qDebug() << "RTLSDRInput::openDevice: " << m_gains.size() << "gains";
}
if ((res = rtlsdr_reset_buffer(m_dev)) < 0)
{
qCritical("RTLSDRInput::openDevice: could not reset USB EP buffers: %s", strerror(errno));
stop();
return false;
}
return true;
}
bool RTLSDRInput::start(int device) // TODO: remove device parameter
{
QMutexLocker mutexLocker(&m_mutex);
if (m_dev != 0)
{
stop();
if (!m_dev) {
return false;
}
char vendor[256];
char product[256];
char serial[256];
int res;
int numberOfGains;
if (!m_sampleFifo.setSize(96000 * 4))
{
qCritical("RTLSDRInput::start: Could not allocate SampleFifo");
return false;
}
if ((res = rtlsdr_open(&m_dev, device)) < 0)
{
qCritical("RTLSDRInput::start: could not open RTLSDR #%d: %s", device, strerror(errno));
return false;
}
vendor[0] = '\0';
product[0] = '\0';
serial[0] = '\0';
if ((res = rtlsdr_get_usb_strings(m_dev, vendor, product, serial)) < 0)
{
qCritical("RTLSDRInput::start: error accessing USB device");
stop();
return false;
}
qWarning("RTLSDRInput::start: open: %s %s, SN: %s", vendor, product, serial);
m_deviceDescription = QString("%1 (SN %2)").arg(product).arg(serial);
if ((res = rtlsdr_set_sample_rate(m_dev, 1152000)) < 0)
{
qCritical("RTLSDRInput::start: could not set sample rate: 1024k S/s");
stop();
return false;
}
if ((res = rtlsdr_set_tuner_gain_mode(m_dev, 1)) < 0)
{
qCritical("RTLSDRInput::start: error setting tuner gain mode");
stop();
return false;
}
if ((res = rtlsdr_set_agc_mode(m_dev, 0)) < 0)
{
qCritical("RTLSDRInput::start: error setting agc mode");
stop();
return false;
}
numberOfGains = rtlsdr_get_tuner_gains(m_dev, NULL);
if (numberOfGains < 0)
{
qCritical("RTLSDRInput::start: error getting number of gain values supported");
stop();
return false;
}
m_gains.resize(numberOfGains);
if (rtlsdr_get_tuner_gains(m_dev, &m_gains[0]) < 0)
{
qCritical("RTLSDRInput::start: error getting gain values");
stop();
return false;
}
else
{
qDebug() << "RTLSDRInput::start: " << m_gains.size() << "gains";
MsgReportRTLSDR *message = MsgReportRTLSDR::create(m_gains);
getOutputMessageQueueToGUI()->push(message);
}
if ((res = rtlsdr_reset_buffer(m_dev)) < 0)
{
qCritical("RTLSDRInput::start: could not reset USB EP buffers: %s", strerror(errno));
stop();
return false;
}
if (m_running) stop();
if ((m_rtlSDRThread = new RTLSDRThread(m_dev, &m_sampleFifo)) == NULL)
{
@@ -144,15 +166,31 @@ bool RTLSDRInput::start(int device)
return false;
}
m_rtlSDRThread->setSamplerate(m_settings.m_devSampleRate);
m_rtlSDRThread->setLog2Decimation(m_settings.m_log2Decim);
m_rtlSDRThread->setFcPos((int) m_settings.m_fcPos);
m_rtlSDRThread->startWork();
mutexLocker.unlock();
applySettings(m_settings, true);
m_running = true;
return true;
}
void RTLSDRInput::closeDevice()
{
if (m_dev != 0)
{
rtlsdr_close(m_dev);
m_dev = 0;
}
m_deviceDescription.clear();
}
void RTLSDRInput::stop()
{
QMutexLocker mutexLocker(&m_mutex);
@@ -164,13 +202,7 @@ void RTLSDRInput::stop()
m_rtlSDRThread = 0;
}
if (m_dev != 0)
{
rtlsdr_close(m_dev);
m_dev = 0;
}
m_deviceDescription.clear();
m_running = false;
}
const QString& RTLSDRInput::getDeviceDescription() const
@@ -205,6 +237,16 @@ bool RTLSDRInput::handleMessage(const Message& message)
return true;
}
else if (MsgQueryRTLSDR::match(message))
{
MsgQueryRTLSDR& conf = (MsgQueryRTLSDR&) message;
qDebug() << "RTLSDRInput::handleMessage: MsgQueryRTLSDR";
MsgReportRTLSDR *message = MsgReportRTLSDR::create(m_gains);
getOutputMessageQueueToGUI()->push(message);
return true;
}
else
{
return false;
@@ -268,7 +310,7 @@ bool RTLSDRInput::applySettings(const RTLSDRSettings& settings, bool force)
}
else
{
m_rtlSDRThread->setSamplerate(settings.m_devSampleRate);
if (m_rtlSDRThread) m_rtlSDRThread->setSamplerate(settings.m_devSampleRate);
qDebug("RTLSDRInput::applySettings: sample rate set to %d", m_settings.m_devSampleRate);
}
}
@@ -279,7 +321,7 @@ bool RTLSDRInput::applySettings(const RTLSDRSettings& settings, bool force)
m_settings.m_log2Decim = settings.m_log2Decim;
forwardChange = true;
if(m_dev != 0)
if (m_rtlSDRThread != 0)
{
m_rtlSDRThread->setLog2Decimation(settings.m_log2Decim);
}
@@ -314,7 +356,7 @@ bool RTLSDRInput::applySettings(const RTLSDRSettings& settings, bool force)
}
}
if(m_dev != 0)
if (m_dev != 0)
{
if (rtlsdr_set_center_freq( m_dev, deviceCenterFrequency ) != 0)
{
@@ -335,7 +377,7 @@ bool RTLSDRInput::applySettings(const RTLSDRSettings& settings, bool force)
{
m_settings.m_fcPos = settings.m_fcPos;
if(m_dev != 0)
if (m_rtlSDRThread != 0)
{
m_rtlSDRThread->setFcPos((int) m_settings.m_fcPos);
qDebug() << "RTLSDRInput: set fc pos (enum) to " << (int) m_settings.m_fcPos;