1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2026-06-02 06:04:39 -04:00

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
+25 -20
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)