From ec3a99451c86b1720424cb7d381f3d595aa19665 Mon Sep 17 00:00:00 2001 From: f4exb Date: Fri, 1 Sep 2017 00:46:43 +0200 Subject: [PATCH] HackRF: use DeviceHackRF as a singleton to handle HackRF library init and exit only once --- devices/hackrf/devicehackrf.cpp | 45 ++++++++++--------- devices/hackrf/devicehackrf.h | 6 +++ .../hackrfoutput/hackrfoutputplugin.cpp | 18 ++++---- .../hackrfinput/hackrfinputplugin.cpp | 19 ++++---- 4 files changed, 50 insertions(+), 38 deletions(-) diff --git a/devices/hackrf/devicehackrf.cpp b/devices/hackrf/devicehackrf.cpp index a7997f69e..f07f5fc57 100644 --- a/devices/hackrf/devicehackrf.cpp +++ b/devices/hackrf/devicehackrf.cpp @@ -17,18 +17,29 @@ #include #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) diff --git a/devices/hackrf/devicehackrf.h b/devices/hackrf/devicehackrf.h index abdb54029..483f6999f 100644 --- a/devices/hackrf/devicehackrf.h +++ b/devices/hackrf/devicehackrf.h @@ -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); }; diff --git a/plugins/samplesink/hackrfoutput/hackrfoutputplugin.cpp b/plugins/samplesink/hackrfoutput/hackrfoutputplugin.cpp index cf9994a0c..a98f53290 100644 --- a/plugins/samplesink/hackrfoutput/hackrfoutputplugin.cpp +++ b/plugins/samplesink/hackrfoutput/hackrfoutputplugin.cpp @@ -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; } diff --git a/plugins/samplesource/hackrfinput/hackrfinputplugin.cpp b/plugins/samplesource/hackrfinput/hackrfinputplugin.cpp index 9a99f1a55..fe7ae1cc6 100644 --- a/plugins/samplesource/hackrfinput/hackrfinputplugin.cpp +++ b/plugins/samplesource/hackrfinput/hackrfinputplugin.cpp @@ -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; }