Transceiver52M: Move reference select from compile time to database

Enabling the external reference on UHD devices through the configure
time switch is awkward. Use a database variable "TRX.Reference" with
'0' or '1' value for internal and external references respectively.
Use internal reference is no entry is defined.

Signed-off-by: Thomas Tsou <tom@tsou.cc>
diff --git a/Transceiver52M/UHDDevice.cpp b/Transceiver52M/UHDDevice.cpp
index 03ff0ca..a392222 100644
--- a/Transceiver52M/UHDDevice.cpp
+++ b/Transceiver52M/UHDDevice.cpp
@@ -214,7 +214,7 @@
 	uhd_device(int sps, bool skip_rx);
 	~uhd_device();
 
-	int open(const std::string &args);
+	int open(const std::string &args, bool extref);
 	bool start();
 	bool stop();
 	void restart(uhd::time_spec_t ts);
@@ -519,7 +519,7 @@
 	return true;
 }
 
-int uhd_device::open(const std::string &args)
+int uhd_device::open(const std::string &args, bool extref)
 {
 	// Find UHD devices
 	uhd::device_addr_t addr(args);
@@ -542,9 +542,9 @@
 	if (!parse_dev_type())
 		return -1;
 
-#ifdef EXTREF
-	set_ref_clk(true);
-#endif
+	if (extref)
+		set_ref_clk(true);
+
 	// Create TX and RX streamers
 	uhd::stream_args_t stream_args("sc16");
 	tx_stream = usrp_dev->get_tx_stream(stream_args);
diff --git a/Transceiver52M/USRPDevice.cpp b/Transceiver52M/USRPDevice.cpp
index 3a61e64..1fff124 100644
--- a/Transceiver52M/USRPDevice.cpp
+++ b/Transceiver52M/USRPDevice.cpp
@@ -90,7 +90,7 @@
 #endif
 }
 
-int USRPDevice::open(const std::string &)
+int USRPDevice::open(const std::string &, bool)
 {
   writeLock.unlock();
 
diff --git a/Transceiver52M/USRPDevice.h b/Transceiver52M/USRPDevice.h
index f74db47..dc86f0e 100644
--- a/Transceiver52M/USRPDevice.h
+++ b/Transceiver52M/USRPDevice.h
@@ -105,7 +105,7 @@
   USRPDevice(int sps, bool skipRx);
 
   /** Instantiate the USRP */
-  int open(const std::string &);
+  int open(const std::string &, bool);
 
   /** Start the USRP */
   bool start();
diff --git a/Transceiver52M/USRPping.cpp b/Transceiver52M/USRPping.cpp
index 0ac5565..3c6289c 100644
--- a/Transceiver52M/USRPping.cpp
+++ b/Transceiver52M/USRPping.cpp
@@ -43,7 +43,7 @@
 
   RadioDevice *usrp = RadioDevice::make(52.0e6/192.0, 1);
 
-  usrp->open("");
+  usrp->open();
 
   TIMESTAMP timestamp;
 
diff --git a/Transceiver52M/radioDevice.h b/Transceiver52M/radioDevice.h
index 07ffd63..e08904b 100644
--- a/Transceiver52M/radioDevice.h
+++ b/Transceiver52M/radioDevice.h
@@ -39,7 +39,7 @@
   static RadioDevice *make(int sps, bool skipRx = false);
 
   /** Initialize the USRP */
-  virtual int open(const std::string &args)=0;
+  virtual int open(const std::string &args = "", bool extref = false)=0;
 
   /** Start the USRP */
   virtual bool start()=0;
diff --git a/Transceiver52M/runTransceiver.cpp b/Transceiver52M/runTransceiver.cpp
index 775424a..6a7cc53 100644
--- a/Transceiver52M/runTransceiver.cpp
+++ b/Transceiver52M/runTransceiver.cpp
@@ -108,7 +108,7 @@
 
 int main(int argc, char *argv[])
 {
-  int trxPort, fail = 0;
+  int trxPort, radioType, extref = 0, fail = 0;
   std::string deviceArgs, logLevel, trxAddr;
   RadioDevice *usrp = NULL;
   RadioInterface *radio = NULL;
@@ -138,12 +138,21 @@
   logLevel = gConfig.getStr("Log.Level");
   trxPort = gConfig.getNum("TRX.Port");
   trxAddr = gConfig.getStr("TRX.IP");
+
+  if (gConfig.defines("TRX.Reference"))
+    extref = gConfig.getNum("TRX.Reference");
+
+  if (extref)
+    std::cout << "Using external clock reference" << std::endl;
+  else
+    std::cout << "Using internal clock reference" << std::endl;
+
   gLogInit("transceiver", logLevel.c_str(), LOG_LOCAL7);
 
   srandom(time(NULL));
 
   usrp = RadioDevice::make(SPS);
-  int radioType = usrp->open(deviceArgs);
+  radioType = usrp->open(deviceArgs, extref);
   if (radioType < 0) {
     LOG(ALERT) << "Transceiver exiting..." << std::endl;
     return EXIT_FAILURE;
diff --git a/configure.ac b/configure.ac
index 1fc58ea..42fcde6 100644
--- a/configure.ac
+++ b/configure.ac
@@ -73,11 +73,6 @@
         [enable single daughterboard use on USRP1])
 ])
 
-AC_ARG_WITH(extref, [
-    AS_HELP_STRING([--with-extref],
-        [enable external reference on UHD devices])
-])
-
 AS_IF([test "x$with_usrp1" = "xyes"], [
     PKG_CHECK_MODULES(USRP, usrp >= 3.3)
     # Find and define supported SIMD extensions
@@ -90,10 +85,6 @@
     AX_EXT
 ])
 
-AS_IF([test "x$with_extref" = "xyes"], [
-    AC_DEFINE(EXTREF, 1, Define to 1 for external reference)
-])
-
 AS_IF([test "x$with_singledb" = "xyes"], [
     AC_DEFINE(SINGLEDB, 1, Define to 1 for single daughterboard)
 ])