osmo-trx: Add a command line option for the dBFS to dBm offset.

Signed-off-by: Tom Tsou <tom.tsou@ettus.com>
diff --git a/Transceiver52M/Transceiver.cpp b/Transceiver52M/Transceiver.cpp
index 4288f1d..71d5d0d 100644
--- a/Transceiver52M/Transceiver.cpp
+++ b/Transceiver52M/Transceiver.cpp
@@ -22,6 +22,7 @@
 */
 
 #include <stdio.h>
+#include <iomanip>      // std::setprecision
 #include "Transceiver.h"
 #include <Logger.h>
 
@@ -140,13 +141,15 @@
 }
 
 Transceiver::Transceiver(int wBasePort,
-			 const char *wTRXAddress,
-			 size_t wSPS, size_t wChans,
-			 GSM::Time wTransmitLatency,
-			 RadioInterface *wRadioInterface)
+                         const char *wTRXAddress,
+                         size_t wSPS, size_t wChans,
+                         GSM::Time wTransmitLatency,
+                         RadioInterface *wRadioInterface,
+                         double wRssiOffset)
   : mBasePort(wBasePort), mAddr(wTRXAddress),
     mClockSocket(wBasePort, wTRXAddress, mBasePort + 100),
     mTransmitLatency(wTransmitLatency), mRadioInterface(wRadioInterface),
+    rssiOffset(wRssiOffset),
     mSPSTx(wSPS), mSPSRx(1), mChans(wChans), mOn(false),
     mTxFreq(0.0), mRxFreq(0.0), mTSC(0), mMaxExpectedDelay(0)
 {
@@ -615,8 +618,8 @@
  * Pull bursts from the FIFO and handle according to the slot
  * and burst correlation type. Equalzation is currently disabled. 
  */
-SoftVector *Transceiver::pullRadioVector(GSM::Time &wTime, int &RSSI,
-                                         int &timingOffset, size_t chan)
+SoftVector *Transceiver::pullRadioVector(GSM::Time &wTime, double &RSSI,
+                                         double &timingOffset, size_t chan)
 {
   bool success, equalize = false;
   complex amp;
@@ -689,8 +692,8 @@
     bits = demodulate(state, *burst, amp, toa, time.TN(), equalize);
 
   wTime = time;
-  RSSI = (int) floor(20.0 * log10(rxFullScale / avg));
-  timingOffset = (int) round(toa * 256.0 / mSPSRx);
+  RSSI = 20.0 * log10(rxFullScale / avg);
+  timingOffset = toa / mSPSRx;
 
   delete radio_burst;
 
@@ -892,27 +895,32 @@
 void Transceiver::driveReceiveFIFO(size_t chan)
 {
   SoftVector *rxBurst = NULL;
-  int RSSI;
-  int TOA;  // in 1/256 of a symbol
+  double RSSI; // in dBFS
+  double dBm;  // in dBm
+  double TOA;  // in symbols
+  int TOAint;  // in 1/256 symbols
   GSM::Time burstTime;
 
   rxBurst = pullRadioVector(burstTime, RSSI, TOA, chan);
 
   if (rxBurst) { 
+    dBm = RSSI+rssiOffset;
+    TOAint = (int) (TOA * 256.0 + 0.5); // round to closest integer
 
-    LOG(DEBUG) << "burst parameters: "
-	  << " time: " << burstTime
-	  << " RSSI: " << RSSI
-	  << " TOA: "  << TOA
-	  << " bits: " << *rxBurst;
+    LOG(DEBUG) << "burst parameters: " << std::fixed
+      << " time: " << burstTime
+      << " RSSI: " << std::setprecision(1) << RSSI
+      << " dBm: "  << std::setprecision(1) << dBm
+      << " TOA: "  << std::setprecision(2) << TOA
+      << " bits: " << *rxBurst;
     
     char burstString[gSlotLen+10];
     burstString[0] = burstTime.TN();
     for (int i = 0; i < 4; i++)
       burstString[1+i] = (burstTime.FN() >> ((3-i)*8)) & 0x0ff;
-    burstString[5] = RSSI;
-    burstString[6] = (TOA >> 8) & 0x0ff;
-    burstString[7] = TOA & 0x0ff;
+    burstString[5] = (int)dBm;
+    burstString[6] = (TOAint >> 8) & 0x0ff;
+    burstString[7] = TOAint & 0x0ff;
     SoftVector::iterator burstItr = rxBurst->begin();
 
     for (unsigned int i = 0; i < gSlotLen; i++) {
diff --git a/Transceiver52M/Transceiver.h b/Transceiver52M/Transceiver.h
index 4f6aa69..e4fcbfa 100644
--- a/Transceiver52M/Transceiver.h
+++ b/Transceiver52M/Transceiver.h
@@ -97,10 +97,11 @@
       @param radioInterface associated radioInterface object
   */
   Transceiver(int wBasePort,
-	      const char *TRXAddress,
-	      size_t wSPS, size_t chans,
-	      GSM::Time wTransmitLatency,
-	      RadioInterface *wRadioInterface);
+              const char *TRXAddress,
+              size_t wSPS, size_t chans,
+              GSM::Time wTransmitLatency,
+              RadioInterface *wRadioInterface,
+              double wRssiOffset);
 
   /** Destructor */
   ~Transceiver();
@@ -181,6 +182,8 @@
   double txFullScale;                     ///< full scale input to radio
   double rxFullScale;                     ///< full scale output to radio
 
+  double rssiOffset;                      ///< RSSI to dBm conversion offset
+
   /** modulate and add a burst to the transmit queue */
   void addRadioVector(size_t chan, BitVector &bits,
                       int RSSI, GSM::Time &wTime);
@@ -192,8 +195,8 @@
   void pushRadioVector(GSM::Time &nowTime);
 
   /** Pull and demodulate a burst from the receive FIFO */
-  SoftVector *pullRadioVector(GSM::Time &wTime, int &RSSI,
-                              int &timingOffset, size_t chan = 0);
+  SoftVector *pullRadioVector(GSM::Time &wTime, double &RSSI,
+                              double &timingOffset, size_t chan = 0);
 
   /** Set modulus for specific timeslot */
   void setModulus(size_t timeslot, size_t chan);
diff --git a/Transceiver52M/osmo-trx.cpp b/Transceiver52M/osmo-trx.cpp
index 3f562ba..048b9f8 100644
--- a/Transceiver52M/osmo-trx.cpp
+++ b/Transceiver52M/osmo-trx.cpp
@@ -70,6 +70,7 @@
 	Transceiver::FillerType filler;
 	bool diversity;
 	double offset;
+	double rssi_offset;
 };
 
 ConfigurationTable gConfig;
@@ -185,6 +186,7 @@
 	ost << "   C0 Filler Table......... " << fillstr << std::endl;
 	ost << "   Diversity............... " << divstr << std::endl;
 	ost << "   Tuning offset........... " << config->offset << std::endl;
+	ost << "   RSSI to dBm offset...... " << config->rssi_offset << std::endl;
 	std::cout << ost << std::endl;
 
 	return true;
@@ -240,7 +242,7 @@
 	VectorFIFO *fifo;
 
 	trx = new Transceiver(config->port, config->addr.c_str(), config->sps,
-			      config->chans, GSM::Time(3,0), radio);
+		config->chans, GSM::Time(3,0), radio, config->rssi_offset);
 	if (!trx->init(config->filler, config->rtsc)) {
 		LOG(ALERT) << "Failed to initialize transceiver";
 		delete trx;
@@ -292,7 +294,8 @@
 		"  -c    Number of ARFCN channels (default=1)\n"
 		"  -f    Enable C0 filler table\n"
 		"  -o    Set baseband frequency offset (default=auto)\n"
-		"  -r    Random burst test mode with TSC\n",
+		"  -r    Random burst test mode with TSC\n"
+		"  -R    RSSI to dBm offset in dB (default=0)\n",
 		"EMERG, ALERT, CRT, ERR, WARNING, NOTICE, INFO, DEBUG");
 }
 
@@ -308,8 +311,9 @@
 	config->filler = Transceiver::FILLER_ZERO;
 	config->diversity = false;
 	config->offset = 0.0;
+	config->rssi_offset = 0.0;
 
-	while ((option = getopt(argc, argv, "ha:l:i:p:c:dxfo:s:r:")) != -1) {
+	while ((option = getopt(argc, argv, "ha:l:i:p:c:dxfo:s:r:R:")) != -1) {
 		switch (option) {
 		case 'h':
 			print_help();
@@ -349,6 +353,9 @@
 			config->rtsc = atoi(optarg);
 			config->filler = Transceiver::FILLER_RAND;
 			break;
+		case 'R':
+			config->rssi_offset = atof(optarg);
+			break;
 		default:
 			print_help();
 			exit(0);