1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-30 03:38:55 -05:00

HackRF input: refactoring: device open close moved in the constructor and destructor respectively of the input object

This commit is contained in:
f4exb 2017-04-14 00:14:40 +02:00
parent afaf3428f5
commit ecdc99f59b
2 changed files with 61 additions and 51 deletions

View File

@ -38,44 +38,34 @@ HackRFInput::HackRFInput(DeviceSourceAPI *deviceAPI) :
m_settings(), m_settings(),
m_dev(0), m_dev(0),
m_hackRFThread(0), m_hackRFThread(0),
m_deviceDescription("HackRF") m_deviceDescription("HackRF"),
m_running(false)
{ {
openDevice();
m_deviceAPI->setBuddySharedPtr(&m_sharedParams); m_deviceAPI->setBuddySharedPtr(&m_sharedParams);
} }
HackRFInput::~HackRFInput() HackRFInput::~HackRFInput()
{ {
if (m_dev != 0) if (m_running) stop();
{ closeDevice();
stop();
}
m_deviceAPI->setBuddySharedPtr(0); m_deviceAPI->setBuddySharedPtr(0);
} }
bool HackRFInput::start(int device) bool HackRFInput::openDevice()
{ {
// QMutexLocker mutexLocker(&m_mutex);
if (m_dev != 0) if (m_dev != 0)
{ {
stop(); closeDevice();
} }
// hackrf_error rc;
//
// rc = (hackrf_error) hackrf_init();
//
// if (rc != HACKRF_SUCCESS)
// {
// qCritical("HackRFInput::start: failed to initiate HackRF library %s", hackrf_error_name(rc));
// }
if (!m_sampleFifo.setSize(1<<19)) if (!m_sampleFifo.setSize(1<<19))
{ {
qCritical("HackRFInput::start: could not allocate SampleFifo"); qCritical("HackRFInput::start: could not allocate SampleFifo");
return false; return false;
} }
int device = m_deviceAPI->getSampleSourceSequence();
if (m_deviceAPI->getSinkBuddies().size() > 0) if (m_deviceAPI->getSinkBuddies().size() > 0)
{ {
@ -84,7 +74,7 @@ bool HackRFInput::start(int device)
if (buddySharedParams == 0) if (buddySharedParams == 0)
{ {
qCritical("HackRFInput::start: could not get shared parameters from buddy"); qCritical("HackRFInput::openDevice: could not get shared parameters from buddy");
return false; return false;
} }
@ -92,7 +82,7 @@ bool HackRFInput::start(int device)
{ {
if ((m_dev = buddySharedParams->m_dev) == 0) // get device handle from Tx but do not take ownership if ((m_dev = buddySharedParams->m_dev) == 0) // get device handle from Tx but do not take ownership
{ {
qCritical("HackRFInput::start: could not get HackRF handle from buddy"); qCritical("HackRFInput::openDevice: could not get HackRF handle from buddy");
return false; return false;
} }
} }
@ -100,7 +90,7 @@ bool HackRFInput::start(int device)
{ {
if ((m_dev = DeviceHackRF::open_hackrf(device)) == 0) if ((m_dev = DeviceHackRF::open_hackrf(device)) == 0)
{ {
qCritical("HackRFInput::start: could not open HackRF #%d", device); qCritical("HackRFInput::openDevice: could not open HackRF #%d", device);
return false; return false;
} }
@ -111,14 +101,27 @@ bool HackRFInput::start(int device)
{ {
if ((m_dev = DeviceHackRF::open_hackrf(device)) == 0) if ((m_dev = DeviceHackRF::open_hackrf(device)) == 0)
{ {
qCritical("HackRFInput::start: could not open HackRF #%d", device); qCritical("HackRFInput::openDevice: could not open HackRF #%d", device);
return false; return false;
} }
m_sharedParams.m_dev = m_dev; m_sharedParams.m_dev = m_dev;
} }
if((m_hackRFThread = new HackRFInputThread(m_dev, &m_sampleFifo)) == 0) return true;
}
bool HackRFInput::start(int device)
{
// QMutexLocker mutexLocker(&m_mutex);
if (m_dev != 0)
{
stop();
}
if (m_running) stop();
if ((m_hackRFThread = new HackRFInputThread(m_dev, &m_sampleFifo)) == 0)
{ {
qFatal("HackRFInput::start: out of memory"); qFatal("HackRFInput::start: out of memory");
stop(); stop();
@ -129,25 +132,20 @@ bool HackRFInput::start(int device)
applySettings(m_settings, true); applySettings(m_settings, true);
m_hackRFThread->setSamplerate(m_settings.m_devSampleRate);
m_hackRFThread->setLog2Decimation(m_settings.m_log2Decim);
m_hackRFThread->setFcPos((int) m_settings.m_fcPos);
m_hackRFThread->startWork(); m_hackRFThread->startWork();
qDebug("HackRFInput::startInput: started"); qDebug("HackRFInput::startInput: started");
m_running = true;
return true; return true;
} }
void HackRFInput::stop() void HackRFInput::closeDevice()
{ {
qDebug("HackRFInput::stop");
// QMutexLocker mutexLocker(&m_mutex);
if(m_hackRFThread != 0)
{
m_hackRFThread->stopWork();
delete m_hackRFThread;
m_hackRFThread = 0;
}
if(m_dev != 0) if(m_dev != 0)
{ {
hackrf_stop_rx(m_dev); hackrf_stop_rx(m_dev);
@ -185,15 +183,21 @@ void HackRFInput::stop()
m_sharedParams.m_dev = 0; m_sharedParams.m_dev = 0;
m_dev = 0; m_dev = 0;
}
// if(m_dev != 0) void HackRFInput::stop()
// { {
// hackrf_stop_rx(m_dev); qDebug("HackRFInput::stop");
// hackrf_close(m_dev); // QMutexLocker mutexLocker(&m_mutex);
// m_dev = 0;
// } if (m_hackRFThread != 0)
// {
// hackrf_exit(); m_hackRFThread->stopWork();
delete m_hackRFThread;
m_hackRFThread = 0;
}
m_running = false;
} }
const QString& HackRFInput::getDeviceDescription() const const QString& HackRFInput::getDeviceDescription() const
@ -285,19 +289,22 @@ bool HackRFInput::applySettings(const HackRFInputSettings& settings, bool force)
qCritical("HackRFInput::applySettings: could not set sample rate TO %llu S/s: %s", m_settings.m_devSampleRate, hackrf_error_name(rc)); qCritical("HackRFInput::applySettings: could not set sample rate TO %llu S/s: %s", m_settings.m_devSampleRate, hackrf_error_name(rc));
} }
else else
{
if (m_hackRFThread != 0)
{ {
qDebug("HackRFInput::applySettings: sample rate set to %llu S/s", m_settings.m_devSampleRate); qDebug("HackRFInput::applySettings: sample rate set to %llu S/s", m_settings.m_devSampleRate);
m_hackRFThread->setSamplerate(m_settings.m_devSampleRate); m_hackRFThread->setSamplerate(m_settings.m_devSampleRate);
} }
} }
} }
}
if ((m_settings.m_log2Decim != settings.m_log2Decim) || force) if ((m_settings.m_log2Decim != settings.m_log2Decim) || force)
{ {
m_settings.m_log2Decim = settings.m_log2Decim; m_settings.m_log2Decim = settings.m_log2Decim;
forwardChange = true; forwardChange = true;
if(m_dev != 0) if (m_hackRFThread != 0)
{ {
m_hackRFThread->setLog2Decimation(m_settings.m_log2Decim); m_hackRFThread->setLog2Decimation(m_settings.m_log2Decim);
qDebug() << "HackRFInput: set decimation to " << (1<<m_settings.m_log2Decim); qDebug() << "HackRFInput: set decimation to " << (1<<m_settings.m_log2Decim);
@ -352,7 +359,7 @@ bool HackRFInput::applySettings(const HackRFInputSettings& settings, bool force)
{ {
m_settings.m_fcPos = settings.m_fcPos; m_settings.m_fcPos = settings.m_fcPos;
if(m_dev != 0) if (m_hackRFThread != 0)
{ {
m_hackRFThread->setFcPos((int) m_settings.m_fcPos); m_hackRFThread->setFcPos((int) m_settings.m_fcPos);
qDebug() << "HackRFInput: set fc pos (enum) to " << (int) m_settings.m_fcPos; qDebug() << "HackRFInput: set fc pos (enum) to " << (int) m_settings.m_fcPos;

View File

@ -81,6 +81,8 @@ public:
virtual bool handleMessage(const Message& message); virtual bool handleMessage(const Message& message);
private: private:
bool openDevice();
void closeDevice();
bool applySettings(const HackRFInputSettings& settings, bool force); bool applySettings(const HackRFInputSettings& settings, bool force);
// hackrf_device *open_hackrf_from_sequence(int sequence); // hackrf_device *open_hackrf_from_sequence(int sequence);
void setCenterFrequency(quint64 freq); void setCenterFrequency(quint64 freq);
@ -92,6 +94,7 @@ private:
HackRFInputThread* m_hackRFThread; HackRFInputThread* m_hackRFThread;
QString m_deviceDescription; QString m_deviceDescription;
DeviceHackRFParams m_sharedParams; DeviceHackRFParams m_sharedParams;
bool m_running;
}; };
#endif // INCLUDE_HACKRFINPUT_H #endif // INCLUDE_HACKRFINPUT_H