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.
This commit is contained in:
Dimitri Stolnikov 2013-10-25 23:36:32 +02:00
parent 55d005e28d
commit a71fbeeaa5
2 changed files with 93 additions and 6 deletions

View File

@ -209,6 +209,7 @@ hackrf_sink_c::hackrf_sink_c (const std::string &args)
<< std::endl; << std::endl;
} }
set_center_freq( (get_freq_range().start() + get_freq_range().stop()) / 2.0 );
set_sample_rate( get_sample_rates().start() ); set_sample_rate( get_sample_rates().start() );
set_bandwidth( 0 ); set_bandwidth( 0 );
@ -449,7 +450,7 @@ std::vector<std::string> hackrf_sink_c::get_devices()
{ {
std::vector<std::string> devices; std::vector<std::string> devices;
std::string label; std::string label;
#if 0
for (unsigned int i = 0; i < 1 /* TODO: missing libhackrf api */; i++) { for (unsigned int i = 0; i < 1 /* TODO: missing libhackrf api */; i++) {
std::string args = "hackrf=" + boost::lexical_cast< std::string >( i ); std::string args = "hackrf=" + boost::lexical_cast< std::string >( i );
@ -462,7 +463,49 @@ std::vector<std::string> hackrf_sink_c::get_devices()
args += ",label='" + label + "'"; args += ",label='" + label + "'";
devices.push_back( args ); 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; return devices;
} }

View File

@ -27,8 +27,8 @@
#include "config.h" #include "config.h"
#endif #endif
#include "hackrf_source_c.h" #include <stdexcept>
#include <gnuradio/io_signature.h> #include <iostream>
#include <boost/assign.hpp> #include <boost/assign.hpp>
#include <boost/format.hpp> #include <boost/format.hpp>
@ -36,8 +36,9 @@
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>
#include <boost/thread/thread.hpp> #include <boost/thread/thread.hpp>
#include <stdexcept> #include <gnuradio/io_signature.h>
#include <iostream>
#include "hackrf_source_c.h"
#include "arg_helpers.h" #include "arg_helpers.h"
@ -163,6 +164,7 @@ hackrf_source_c::hackrf_source_c (const std::string &args)
<< std::endl; << std::endl;
} }
set_center_freq( (get_freq_range().start() + get_freq_range().stop()) / 2.0 );
set_sample_rate( get_sample_rates().start() ); set_sample_rate( get_sample_rates().start() );
set_bandwidth( 0 ); set_bandwidth( 0 );
@ -341,7 +343,7 @@ std::vector<std::string> hackrf_source_c::get_devices()
{ {
std::vector<std::string> devices; std::vector<std::string> devices;
std::string label; std::string label;
#if 0
for (unsigned int i = 0; i < 1 /* TODO: missing libhackrf api */; i++) { for (unsigned int i = 0; i < 1 /* TODO: missing libhackrf api */; i++) {
std::string args = "hackrf=" + boost::lexical_cast< std::string >( i ); std::string args = "hackrf=" + boost::lexical_cast< std::string >( i );
@ -354,7 +356,49 @@ std::vector<std::string> hackrf_source_c::get_devices()
args += ",label='" + label + "'"; args += ",label='" + label + "'";
devices.push_back( args ); 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; return devices;
} }