mirror of
https://github.com/f4exb/sdrangel.git
synced 2025-06-02 06:52:29 -04:00
HackRF: use DeviceHackRF as a singleton to handle HackRF library init and exit only once
This commit is contained in:
parent
afe08a3547
commit
ec3a99451c
@ -17,18 +17,29 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "devicehackrf.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_device *DeviceHackRF::open_hackrf(int sequence)
|
||||||
{
|
{
|
||||||
hackrf_error rc;
|
instance();
|
||||||
|
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
return open_hackrf_from_sequence(sequence);
|
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_device *DeviceHackRF::open_hackrf(const char * const serial)
|
||||||
{
|
{
|
||||||
hackrf_error rc;
|
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;
|
hackrf_device *hackrf_ptr;
|
||||||
|
|
||||||
|
instance();
|
||||||
|
|
||||||
rc = (hackrf_error) hackrf_open_by_serial(serial, &hackrf_ptr);
|
rc = (hackrf_error) hackrf_open_by_serial(serial, &hackrf_ptr);
|
||||||
|
|
||||||
if (rc == HACKRF_SUCCESS)
|
if (rc == HACKRF_SUCCESS)
|
||||||
@ -66,6 +69,8 @@ hackrf_device *DeviceHackRF::open_hackrf_from_sequence(int sequence)
|
|||||||
hackrf_device *hackrf_ptr;
|
hackrf_device *hackrf_ptr;
|
||||||
hackrf_error rc;
|
hackrf_error rc;
|
||||||
|
|
||||||
|
instance();
|
||||||
|
|
||||||
rc = (hackrf_error) hackrf_device_list_open(hackrf_devices, sequence, &hackrf_ptr);
|
rc = (hackrf_error) hackrf_device_list_open(hackrf_devices, sequence, &hackrf_ptr);
|
||||||
|
|
||||||
if (rc == HACKRF_SUCCESS)
|
if (rc == HACKRF_SUCCESS)
|
||||||
|
@ -22,8 +22,14 @@
|
|||||||
class DeviceHackRF
|
class DeviceHackRF
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
static DeviceHackRF& instance();
|
||||||
static hackrf_device *open_hackrf(int sequence);
|
static hackrf_device *open_hackrf(int sequence);
|
||||||
static hackrf_device *open_hackrf(const char * const serial);
|
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:
|
private:
|
||||||
static hackrf_device *open_hackrf_from_sequence(int sequence);
|
static hackrf_device *open_hackrf_from_sequence(int sequence);
|
||||||
};
|
};
|
||||||
|
@ -56,12 +56,12 @@ void HackRFOutputPlugin::initPlugin(PluginAPI* pluginAPI)
|
|||||||
|
|
||||||
PluginInterface::SamplingDevices HackRFOutputPlugin::enumSampleSinks()
|
PluginInterface::SamplingDevices HackRFOutputPlugin::enumSampleSinks()
|
||||||
{
|
{
|
||||||
hackrf_error rc = (hackrf_error) hackrf_init();
|
// hackrf_error rc = (hackrf_error) hackrf_init();
|
||||||
|
//
|
||||||
if (rc != HACKRF_SUCCESS)
|
// if (rc != HACKRF_SUCCESS)
|
||||||
{
|
// {
|
||||||
qCritical("HackRFOutputPlugin::enumSampleSinks: failed to initiate HackRF library: %s", hackrf_error_name(rc));
|
// qCritical("HackRFOutputPlugin::enumSampleSinks: failed to initiate HackRF library: %s", hackrf_error_name(rc));
|
||||||
}
|
// }
|
||||||
|
|
||||||
SamplingDevices result;
|
SamplingDevices result;
|
||||||
hackrf_device_list_t *hackrf_devices = hackrf_device_list();
|
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++)
|
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)
|
if (rc == HACKRF_SUCCESS)
|
||||||
{
|
{
|
||||||
@ -110,8 +110,8 @@ PluginInterface::SamplingDevices HackRFOutputPlugin::enumSampleSinks()
|
|||||||
}
|
}
|
||||||
|
|
||||||
hackrf_device_list_free(hackrf_devices);
|
hackrf_device_list_free(hackrf_devices);
|
||||||
rc = (hackrf_error) hackrf_exit();
|
// rc = (hackrf_error) hackrf_exit();
|
||||||
qDebug("HackRFOutputPlugin::enumSampleSinks: hackrf_exit: %s", hackrf_error_name(rc));
|
// qDebug("HackRFOutputPlugin::enumSampleSinks: hackrf_exit: %s", hackrf_error_name(rc));
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -56,12 +56,13 @@ void HackRFInputPlugin::initPlugin(PluginAPI* pluginAPI)
|
|||||||
|
|
||||||
PluginInterface::SamplingDevices HackRFInputPlugin::enumSampleSources()
|
PluginInterface::SamplingDevices HackRFInputPlugin::enumSampleSources()
|
||||||
{
|
{
|
||||||
hackrf_error rc = (hackrf_error) hackrf_init();
|
DeviceHackRF::instance();
|
||||||
|
// hackrf_error rc = (hackrf_error) hackrf_init();
|
||||||
if (rc != HACKRF_SUCCESS)
|
//
|
||||||
{
|
// if (rc != HACKRF_SUCCESS)
|
||||||
qCritical("HackRFPlugin::SampleSourceDevices: failed to initiate HackRF library: %s", hackrf_error_name(rc));
|
// {
|
||||||
}
|
// qCritical("HackRFPlugin::SampleSourceDevices: failed to initiate HackRF library: %s", hackrf_error_name(rc));
|
||||||
|
// }
|
||||||
|
|
||||||
SamplingDevices result;
|
SamplingDevices result;
|
||||||
hackrf_device_list_t *hackrf_devices = hackrf_device_list();
|
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++)
|
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)
|
if (rc == HACKRF_SUCCESS)
|
||||||
{
|
{
|
||||||
@ -110,8 +111,8 @@ PluginInterface::SamplingDevices HackRFInputPlugin::enumSampleSources()
|
|||||||
}
|
}
|
||||||
|
|
||||||
hackrf_device_list_free(hackrf_devices);
|
hackrf_device_list_free(hackrf_devices);
|
||||||
rc = (hackrf_error) hackrf_exit();
|
// rc = (hackrf_error) hackrf_exit();
|
||||||
qDebug("HackRFPlugin::enumSampleSources: hackrf_exit: %s", hackrf_error_name(rc));
|
// qDebug("HackRFPlugin::enumSampleSources: hackrf_exit: %s", hackrf_error_name(rc));
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user