HackRF: use DeviceHackRF as a singleton to handle HackRF library init and exit only once

This commit is contained in:
f4exb 2017-09-01 00:46:43 +02:00
parent afe08a3547
commit ec3a99451c
4 changed files with 50 additions and 38 deletions

View File

@ -17,18 +17,29 @@
#include <stdio.h>
#include "devicehackrf.h"
DeviceHackRF::DeviceHackRF()
{
hackrf_error 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));
}
}
DeviceHackRF::~DeviceHackRF()
{
hackrf_exit();
}
DeviceHackRF& DeviceHackRF::instance()
{
static DeviceHackRF inst;
return inst;
}
hackrf_device *DeviceHackRF::open_hackrf(int sequence)
{
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;
}
instance();
return open_hackrf_from_sequence(sequence);
}
@ -36,18 +47,10 @@ hackrf_device *DeviceHackRF::open_hackrf(int 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;
instance();
rc = (hackrf_error) hackrf_open_by_serial(serial, &hackrf_ptr);
if (rc == HACKRF_SUCCESS)
@ -66,6 +69,8 @@ hackrf_device *DeviceHackRF::open_hackrf_from_sequence(int sequence)
hackrf_device *hackrf_ptr;
hackrf_error rc;
instance();
rc = (hackrf_error) hackrf_device_list_open(hackrf_devices, sequence, &hackrf_ptr);
if (rc == HACKRF_SUCCESS)

View File

@ -22,8 +22,14 @@
class DeviceHackRF
{
public:
static DeviceHackRF& instance();
static hackrf_device *open_hackrf(int sequence);
static hackrf_device *open_hackrf(const char * const serial);
protected:
DeviceHackRF();
DeviceHackRF(const DeviceHackRF&) {}
DeviceHackRF& operator=(const DeviceHackRF& other __attribute__((unused))) { return *this; }
~DeviceHackRF();
private:
static hackrf_device *open_hackrf_from_sequence(int sequence);
};

View File

@ -56,12 +56,12 @@ void HackRFOutputPlugin::initPlugin(PluginAPI* pluginAPI)
PluginInterface::SamplingDevices HackRFOutputPlugin::enumSampleSinks()
{
hackrf_error rc = (hackrf_error) hackrf_init();
if (rc != HACKRF_SUCCESS)
{
qCritical("HackRFOutputPlugin::enumSampleSinks: failed to initiate HackRF library: %s", hackrf_error_name(rc));
}
// hackrf_error rc = (hackrf_error) hackrf_init();
//
// if (rc != HACKRF_SUCCESS)
// {
// qCritical("HackRFOutputPlugin::enumSampleSinks: failed to initiate HackRF library: %s", hackrf_error_name(rc));
// }
SamplingDevices result;
hackrf_device_list_t *hackrf_devices = hackrf_device_list();
@ -71,7 +71,7 @@ PluginInterface::SamplingDevices HackRFOutputPlugin::enumSampleSinks()
for (i=0; i < hackrf_devices->devicecount; i++)
{
rc = (hackrf_error) hackrf_device_list_open(hackrf_devices, i, &hackrf_ptr);
hackrf_error rc = (hackrf_error) hackrf_device_list_open(hackrf_devices, i, &hackrf_ptr);
if (rc == HACKRF_SUCCESS)
{
@ -110,8 +110,8 @@ PluginInterface::SamplingDevices HackRFOutputPlugin::enumSampleSinks()
}
hackrf_device_list_free(hackrf_devices);
rc = (hackrf_error) hackrf_exit();
qDebug("HackRFOutputPlugin::enumSampleSinks: hackrf_exit: %s", hackrf_error_name(rc));
// rc = (hackrf_error) hackrf_exit();
// qDebug("HackRFOutputPlugin::enumSampleSinks: hackrf_exit: %s", hackrf_error_name(rc));
return result;
}

View File

@ -56,12 +56,13 @@ void HackRFInputPlugin::initPlugin(PluginAPI* pluginAPI)
PluginInterface::SamplingDevices HackRFInputPlugin::enumSampleSources()
{
hackrf_error rc = (hackrf_error) hackrf_init();
if (rc != HACKRF_SUCCESS)
{
qCritical("HackRFPlugin::SampleSourceDevices: failed to initiate HackRF library: %s", hackrf_error_name(rc));
}
DeviceHackRF::instance();
// hackrf_error rc = (hackrf_error) hackrf_init();
//
// if (rc != HACKRF_SUCCESS)
// {
// qCritical("HackRFPlugin::SampleSourceDevices: failed to initiate HackRF library: %s", hackrf_error_name(rc));
// }
SamplingDevices result;
hackrf_device_list_t *hackrf_devices = hackrf_device_list();
@ -71,7 +72,7 @@ PluginInterface::SamplingDevices HackRFInputPlugin::enumSampleSources()
for (i=0; i < hackrf_devices->devicecount; i++)
{
rc = (hackrf_error) hackrf_device_list_open(hackrf_devices, i, &hackrf_ptr);
hackrf_error rc = (hackrf_error) hackrf_device_list_open(hackrf_devices, i, &hackrf_ptr);
if (rc == HACKRF_SUCCESS)
{
@ -110,8 +111,8 @@ PluginInterface::SamplingDevices HackRFInputPlugin::enumSampleSources()
}
hackrf_device_list_free(hackrf_devices);
rc = (hackrf_error) hackrf_exit();
qDebug("HackRFPlugin::enumSampleSources: hackrf_exit: %s", hackrf_error_name(rc));
// rc = (hackrf_error) hackrf_exit();
// qDebug("HackRFPlugin::enumSampleSources: hackrf_exit: %s", hackrf_error_name(rc));
return result;
}