From a71fbeeaa58b7591886d29401610eb78faf3c99d Mon Sep 17 00:00:00 2001 From: Dimitri Stolnikov Date: Fri, 25 Oct 2013 23:36:32 +0200 Subject: [PATCH] hackrf: implement device discovery Unfortunately libhackrf still doesn't offer a way to enumerate devices *or* to open a specific device by index or it's serial number. Thus we have implemented a rather hack-ish way to detect the presence of a device by trying to open it and closing right after that. --- lib/hackrf/hackrf_sink_c.cc | 45 ++++++++++++++++++++++++++++- lib/hackrf/hackrf_source_c.cc | 54 +++++++++++++++++++++++++++++++---- 2 files changed, 93 insertions(+), 6 deletions(-) diff --git a/lib/hackrf/hackrf_sink_c.cc b/lib/hackrf/hackrf_sink_c.cc index eed0876..04a7124 100644 --- a/lib/hackrf/hackrf_sink_c.cc +++ b/lib/hackrf/hackrf_sink_c.cc @@ -209,6 +209,7 @@ hackrf_sink_c::hackrf_sink_c (const std::string &args) << std::endl; } + set_center_freq( (get_freq_range().start() + get_freq_range().stop()) / 2.0 ); set_sample_rate( get_sample_rates().start() ); set_bandwidth( 0 ); @@ -449,7 +450,7 @@ std::vector hackrf_sink_c::get_devices() { std::vector devices; std::string label; - +#if 0 for (unsigned int i = 0; i < 1 /* TODO: missing libhackrf api */; i++) { std::string args = "hackrf=" + boost::lexical_cast< std::string >( i ); @@ -462,7 +463,49 @@ std::vector hackrf_sink_c::get_devices() args += ",label='" + label + "'"; devices.push_back( args ); } +#else + { + boost::mutex::scoped_lock lock( _usage_mutex ); + + if ( _usage == 0 ) + hackrf_init(); /* call only once before the first open */ + + _usage++; + } + + int ret; + hackrf_device *dev = NULL; + ret = hackrf_open(&dev); + if ( HACKRF_SUCCESS == ret ) + { + std::string args = "hackrf=0"; + + label = "HackRF"; + + uint8_t board_id; + ret = hackrf_board_id_read( dev, &board_id ); + if ( HACKRF_SUCCESS == ret ) + { + label += std::string(" ") + hackrf_board_id_name(hackrf_board_id(board_id)); + } + + args += ",label='" + label + "'"; + devices.push_back( args ); + + ret = hackrf_close(dev); + } + + { + boost::mutex::scoped_lock lock( _usage_mutex ); + + _usage--; + + if ( _usage == 0 ) + hackrf_exit(); /* call only once after last close */ + } + +#endif return devices; } diff --git a/lib/hackrf/hackrf_source_c.cc b/lib/hackrf/hackrf_source_c.cc index 2883435..795a3eb 100644 --- a/lib/hackrf/hackrf_source_c.cc +++ b/lib/hackrf/hackrf_source_c.cc @@ -27,8 +27,8 @@ #include "config.h" #endif -#include "hackrf_source_c.h" -#include +#include +#include #include #include @@ -36,8 +36,9 @@ #include #include -#include -#include +#include + +#include "hackrf_source_c.h" #include "arg_helpers.h" @@ -163,6 +164,7 @@ hackrf_source_c::hackrf_source_c (const std::string &args) << std::endl; } + set_center_freq( (get_freq_range().start() + get_freq_range().stop()) / 2.0 ); set_sample_rate( get_sample_rates().start() ); set_bandwidth( 0 ); @@ -341,7 +343,7 @@ std::vector hackrf_source_c::get_devices() { std::vector devices; std::string label; - +#if 0 for (unsigned int i = 0; i < 1 /* TODO: missing libhackrf api */; i++) { std::string args = "hackrf=" + boost::lexical_cast< std::string >( i ); @@ -354,7 +356,49 @@ std::vector hackrf_source_c::get_devices() args += ",label='" + label + "'"; devices.push_back( args ); } +#else + { + boost::mutex::scoped_lock lock( _usage_mutex ); + + if ( _usage == 0 ) + hackrf_init(); /* call only once before the first open */ + + _usage++; + } + + int ret; + hackrf_device *dev = NULL; + ret = hackrf_open(&dev); + if ( HACKRF_SUCCESS == ret ) + { + std::string args = "hackrf=0"; + + label = "HackRF"; + + uint8_t board_id; + ret = hackrf_board_id_read( dev, &board_id ); + if ( HACKRF_SUCCESS == ret ) + { + label += std::string(" ") + hackrf_board_id_name(hackrf_board_id(board_id)); + } + + args += ",label='" + label + "'"; + devices.push_back( args ); + + ret = hackrf_close(dev); + } + + { + boost::mutex::scoped_lock lock( _usage_mutex ); + + _usage--; + + if ( _usage == 0 ) + hackrf_exit(); /* call only once after last close */ + } + +#endif return devices; }