aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDimitri Stolnikov <horiz0n@gmx.net>2013-10-25 23:36:32 +0200
committerDimitri Stolnikov <horiz0n@gmx.net>2013-10-25 23:49:24 +0200
commit6252b4144a10f0971f2620eefadb60b0747b0257 (patch)
tree8b24ec6e9e484922316dc3ccf80ff4baac72e19a
parente394dd39220c3d8b3cb3bd1aaa3986149c590a12 (diff)
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.
-rw-r--r--lib/hackrf/hackrf_sink_c.cc45
-rw-r--r--lib/hackrf/hackrf_source_c.cc54
2 files changed, 93 insertions, 6 deletions
diff --git a/lib/hackrf/hackrf_sink_c.cc b/lib/hackrf/hackrf_sink_c.cc
index 4f2cd28..f52b3ff 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<std::string> hackrf_sink_c::get_devices()
{
std::vector<std::string> 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<std::string> 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 c8ff0b5..fc11020 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 <gnuradio/gr_io_signature.h>
+#include <stdexcept>
+#include <iostream>
#include <boost/assign.hpp>
#include <boost/format.hpp>
@@ -36,8 +36,9 @@
#include <boost/algorithm/string.hpp>
#include <boost/thread/thread.hpp>
-#include <stdexcept>
-#include <iostream>
+#include <gnuradio/gr_io_signature.h>
+
+#include "hackrf_source_c.h"
#include <osmosdr_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<std::string> hackrf_source_c::get_devices()
{
std::vector<std::string> 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<std::string> 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;
}