From 00352d0c5001590f1267b74f9492b51dccca7935 Mon Sep 17 00:00:00 2001
From: Mike Davis <dodgymike@gmail.com>
Date: Mon, 29 Feb 2016 12:48:10 +0200
Subject: [PATCH 2/3] Moved hackrf device-open logic to common function for
 hackrf_{sink,source}

---
 lib/hackrf/CMakeLists.txt     |  1 +
 lib/hackrf/hackrf_open.cc     | 68 +++++++++++++++++++++++++++++++++++++++++++
 lib/hackrf/hackrf_open.h      | 37 +++++++++++++++++++++++
 lib/hackrf/hackrf_sink_c.cc   |  8 ++---
 lib/hackrf/hackrf_sink_c.h    |  1 +
 lib/hackrf/hackrf_source_c.cc | 36 ++++-------------------
 lib/hackrf/hackrf_source_c.h  |  2 ++
 7 files changed, 116 insertions(+), 37 deletions(-)
 create mode 100644 lib/hackrf/hackrf_open.cc
 create mode 100644 lib/hackrf/hackrf_open.h

diff --git a/lib/hackrf/CMakeLists.txt b/lib/hackrf/CMakeLists.txt
index c7af0c9..3324dbc 100644
--- a/lib/hackrf/CMakeLists.txt
+++ b/lib/hackrf/CMakeLists.txt
@@ -29,6 +29,7 @@ include_directories(
 set(hackrf_srcs
     ${CMAKE_CURRENT_SOURCE_DIR}/hackrf_source_c.cc
     ${CMAKE_CURRENT_SOURCE_DIR}/hackrf_sink_c.cc
+    ${CMAKE_CURRENT_SOURCE_DIR}/hackrf_open.cc
 )
 
 INCLUDE(CheckFunctionExists)
diff --git a/lib/hackrf/hackrf_open.cc b/lib/hackrf/hackrf_open.cc
new file mode 100644
index 0000000..045d345
--- /dev/null
+++ b/lib/hackrf/hackrf_open.cc
@@ -0,0 +1,68 @@
+/* -*- c++ -*- */
+/*
+ * Copyright 2013 Dimitri Stolnikov <horiz0n@gmx.net>
+ *
+ * GNU Radio is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3, or (at your option)
+ * any later version.
+ *
+ * GNU Radio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GNU Radio; see the file COPYING.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street,
+ * Boston, MA 02110-1301, USA.
+ */
+
+/*
+ * config.h is generated by configure.  It contains the results
+ * of probing for features, options etc.  It should be the first
+ * file included in your .cc file.
+ */
+#include "hackrf_open.h"
+
+#include <boost/lexical_cast.hpp>
+
+int hackrf_device_open(std::string * hackrf_serial, hackrf_device** _dev) {
+  int ret;
+
+#ifdef LIBHACKRF_HAVE_DEVICE_LIST
+  if ((hackrf_serial != NULL) && hackrf_serial->length() > 0) {
+    if (hackrf_serial->length() > 1) {
+      ret = hackrf_open_by_serial( hackrf_serial->c_str(), _dev );
+    } else {
+        int dev_index = 0;
+        try {
+          dev_index = boost::lexical_cast< int >( *hackrf_serial );
+        } catch ( std::exception &ex ) {
+          throw std::runtime_error(
+                "Failed to use '" + *hackrf_serial + "' as HackRF device index number: " + ex.what());
+        }
+
+        hackrf_device_list_t *list = hackrf_device_list();
+
+        std::cerr << "Found " << list->devicecount << " devices in hackrf device list" << std::endl;
+        std::cerr << "Provided device index: " << dev_index << std::endl;
+
+        if (dev_index < list->devicecount) {
+          ret = hackrf_device_list_open(list, dev_index, _dev);
+
+          std::cerr << "Device open rv: " << ret << std::endl;
+        } else {
+          hackrf_device_list_free(list);
+          throw std::runtime_error(
+                "Failed to use '" + *hackrf_serial + "' as HackRF device index: not enough devices");
+        }
+        hackrf_device_list_free(list);
+    }
+  } else
+#endif
+    ret = hackrf_open( _dev );
+    
+  return ret;
+}
+
diff --git a/lib/hackrf/hackrf_open.h b/lib/hackrf/hackrf_open.h
new file mode 100644
index 0000000..29b3be5
--- /dev/null
+++ b/lib/hackrf/hackrf_open.h
@@ -0,0 +1,37 @@
+/* -*- c++ -*- */
+/*
+ * Copyright 2013 Dimitri Stolnikov <horiz0n@gmx.net>
+ *
+ * This file is part of GNU Radio
+ *
+ * GNU Radio is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3, or (at your option)
+ * any later version.
+ *
+ * GNU Radio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GNU Radio; see the file COPYING.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street,
+ * Boston, MA 02110-1301, USA.
+ */
+#ifndef INCLUDED_HACKRF_OPEN_H
+#define INCLUDED_HACKRF_OPEN_H
+
+#include <gnuradio/thread/thread.h>
+#include <gnuradio/sync_block.h>
+
+#include <boost/thread/mutex.hpp>
+#include <boost/thread/condition_variable.hpp>
+
+#include <libhackrf/hackrf.h>
+
+#include "source_iface.h"
+
+int hackrf_device_open(std::string * hackrf_serial, hackrf_device** _dev);
+
+#endif /* INCLUDED_HACKRF_OPEN_H */
diff --git a/lib/hackrf/hackrf_sink_c.cc b/lib/hackrf/hackrf_sink_c.cc
index 8ca1b3e..207a19d 100644
--- a/lib/hackrf/hackrf_sink_c.cc
+++ b/lib/hackrf/hackrf_sink_c.cc
@@ -191,12 +191,8 @@ hackrf_sink_c::hackrf_sink_c (const std::string &args)
   }
 
   _dev = NULL;
-#ifdef LIBHACKRF_HAVE_DEVICE_LIST
-  if ( hackrf_serial )
-    ret = hackrf_open_by_serial( hackrf_serial->c_str(), &_dev );
-  else
-#endif  
-    ret = hackrf_open( &_dev );
+  ret = hackrf_device_open(hackrf_serial, &_dev);
+
   HACKRF_THROW_ON_ERROR(ret, "Failed to open HackRF device")
 
   uint8_t board_id;
diff --git a/lib/hackrf/hackrf_sink_c.h b/lib/hackrf/hackrf_sink_c.h
index a7e7ab8..fafbed7 100644
--- a/lib/hackrf/hackrf_sink_c.h
+++ b/lib/hackrf/hackrf_sink_c.h
@@ -29,6 +29,7 @@
 #include <libhackrf/hackrf.h>
 
 #include "sink_iface.h"
+#include "hackrf_open.h"
 
 class hackrf_sink_c;
 
diff --git a/lib/hackrf/hackrf_source_c.cc b/lib/hackrf/hackrf_source_c.cc
index 4211603..80e8f51 100644
--- a/lib/hackrf/hackrf_source_c.cc
+++ b/lib/hackrf/hackrf_source_c.cc
@@ -102,8 +102,6 @@ hackrf_source_c::hackrf_source_c (const std::string &args)
     _bandwidth(0)
 {
   int ret;
-  std::string hackrf_serial;
-
   dict_t dict = params_to_dict(args);
 
   _buf_num = _buf_len = _buf_head = _buf_used = _buf_offset = 0;
@@ -143,35 +141,11 @@ hackrf_source_c::hackrf_source_c (const std::string &args)
   }
 
   _dev = NULL;
-  
-#ifdef LIBHACKRF_HAVE_DEVICE_LIST
-  if (dict.count("hackrf") && dict["hackrf"].length() > 0) {
-    hackrf_serial = dict["hackrf"];
-    
-    if (hackrf_serial.length() > 1) {
-      ret = hackrf_open_by_serial( hackrf_serial.c_str(), &_dev );
-    } else {
-        int dev_index = 0;
-        try {
-          dev_index = boost::lexical_cast< int >( hackrf_serial );
-        } catch ( std::exception &ex ) {
-          throw std::runtime_error(
-                "Failed to use '" + hackrf_serial + "' as HackRF device index number: " + ex.what());
-        }
-
-        hackrf_device_list_t *list = hackrf_device_list();
-        if (dev_index < list->devicecount) {
-          ret = hackrf_device_list_open(list, dev_index, &_dev);
-        } else {
-          hackrf_device_list_free(list);
-          throw std::runtime_error(
-                "Failed to use '" + hackrf_serial + "' as HackRF device index: not enough devices");
-        }
-        hackrf_device_list_free(list);
-    }
-  } else
-#endif
-    ret = hackrf_open( &_dev );
+
+  //hackrf_device * hackrf_device_open(std::string hackrd_serial, hackrf_device** _dev) {
+  std::string* hackrf_serial = dict.count("hackrf") ? &(dict["hackrf"]) : NULL;
+  ret = hackrf_device_open(hackrf_serial, &_dev);
+
     
   HACKRF_THROW_ON_ERROR(ret, "Failed to open HackRF device")
 
diff --git a/lib/hackrf/hackrf_source_c.h b/lib/hackrf/hackrf_source_c.h
index 6ae81d2..2f42fff 100644
--- a/lib/hackrf/hackrf_source_c.h
+++ b/lib/hackrf/hackrf_source_c.h
@@ -32,6 +32,8 @@
 
 #include "source_iface.h"
 
+#include "hackrf_open.h"
+
 class hackrf_source_c;
 
 /*
-- 
2.7.4

