diff --git a/devices/hackrf/devicehackrf.cpp b/devices/hackrf/devicehackrf.cpp index bebfb58f9..a7997f69e 100644 --- a/devices/hackrf/devicehackrf.cpp +++ b/devices/hackrf/devicehackrf.cpp @@ -33,6 +33,33 @@ hackrf_device *DeviceHackRF::open_hackrf(int sequence) return open_hackrf_from_sequence(sequence); } +hackrf_device *DeviceHackRF::open_hackrf(const char * const serial) +{ + hackrf_error rc; + + // TODO: this may not work if several HackRF Devices are running concurrently. It should be handled globally in the application + rc = (hackrf_error) hackrf_init(); + + if (rc != HACKRF_SUCCESS) + { + fprintf(stderr, "DeviceHackRF::open_hackrf: failed to initiate HackRF library %s\n", hackrf_error_name(rc)); + return 0; + } + + hackrf_device *hackrf_ptr; + + rc = (hackrf_error) hackrf_open_by_serial(serial, &hackrf_ptr); + + if (rc == HACKRF_SUCCESS) + { + return hackrf_ptr; + } + else + { + return 0; + } +} + hackrf_device *DeviceHackRF::open_hackrf_from_sequence(int sequence) { hackrf_device_list_t *hackrf_devices = hackrf_device_list(); diff --git a/devices/hackrf/devicehackrf.h b/devices/hackrf/devicehackrf.h index d645f95cb..abdb54029 100644 --- a/devices/hackrf/devicehackrf.h +++ b/devices/hackrf/devicehackrf.h @@ -23,6 +23,7 @@ class DeviceHackRF { public: static hackrf_device *open_hackrf(int sequence); + static hackrf_device *open_hackrf(const char * const serial); private: static hackrf_device *open_hackrf_from_sequence(int sequence); }; diff --git a/plugins/samplesink/hackrfoutput/hackrfoutput.cpp b/plugins/samplesink/hackrfoutput/hackrfoutput.cpp index e1963b375..4fa02e342 100644 --- a/plugins/samplesink/hackrfoutput/hackrfoutput.cpp +++ b/plugins/samplesink/hackrfoutput/hackrfoutput.cpp @@ -60,7 +60,6 @@ bool HackRFOutput::openDevice() } m_sampleSourceFifo.resize(m_settings.m_devSampleRate/(1<<(m_settings.m_log2Interp <= 4 ? m_settings.m_log2Interp : 4))); - int device = m_deviceAPI->getSampleSinkSequence(); if (m_deviceAPI->getSourceBuddies().size() > 0) { @@ -84,9 +83,9 @@ bool HackRFOutput::openDevice() } else { - if ((m_dev = DeviceHackRF::open_hackrf(device)) == 0) + if ((m_dev = DeviceHackRF::open_hackrf(qPrintable(m_deviceAPI->getSampleSinkSerial()))) == 0) { - qCritical("HackRFOutput::openDevice: could not open HackRF #%d", device); + qCritical("HackRFOutput::openDevice: could not open HackRF %s", qPrintable(m_deviceAPI->getSampleSinkSerial())); return false; } diff --git a/plugins/samplesource/hackrfinput/hackrfinput.cpp b/plugins/samplesource/hackrfinput/hackrfinput.cpp index ee1f7cf37..9c183e3b2 100644 --- a/plugins/samplesource/hackrfinput/hackrfinput.cpp +++ b/plugins/samplesource/hackrfinput/hackrfinput.cpp @@ -65,8 +65,6 @@ bool HackRFInput::openDevice() return false; } - int device = m_deviceAPI->getSampleSourceSequence(); - if (m_deviceAPI->getSinkBuddies().size() > 0) { DeviceSinkAPI *buddy = m_deviceAPI->getSinkBuddies()[0]; @@ -89,9 +87,9 @@ bool HackRFInput::openDevice() } else { - if ((m_dev = DeviceHackRF::open_hackrf(device)) == 0) + if ((m_dev = DeviceHackRF::open_hackrf(qPrintable(m_deviceAPI->getSampleSourceSerial()))) == 0) { - qCritical("HackRFInput::openDevice: could not open HackRF #%d", device); + qCritical("HackRFInput::openDevice: could not open HackRF %s", qPrintable(m_deviceAPI->getSampleSourceSerial())); return false; }