osmo-trx: Add osmo_signal to stop whole transceiver chain correctly on error

Transceiver::stop() can only be called from either CTRL iface thread or
from main thread (running osmocom loop). That's because stop attempts to
cancel and then join all the other threads, which would then lock if
attempting to stop from some of them.
As a result, the best option is to indicate to the user of the
transceiver option (osmo-trx.cpp) to stop it in a correct fashion by
destroying the object from the main thread.

Change-Id: Iac1d2dbe2328e735db2d4b933cb67b1af1babca1
diff --git a/Transceiver52M/Transceiver.cpp b/Transceiver52M/Transceiver.cpp
index a1ebb30..cdfd79d 100644
--- a/Transceiver52M/Transceiver.cpp
+++ b/Transceiver52M/Transceiver.cpp
@@ -27,6 +27,10 @@
 #include "Transceiver.h"
 #include <Logger.h>
 
+extern "C" {
+#include "osmo_signal.h"
+}
+
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
@@ -115,7 +119,7 @@
   : mBasePort(wBasePort), mLocalAddr(TRXAddress), mRemoteAddr(GSMcoreAddress),
     mClockSocket(TRXAddress, wBasePort, GSMcoreAddress, wBasePort + 100),
     mTransmitLatency(wTransmitLatency), mRadioInterface(wRadioInterface),
-    rssiOffset(wRssiOffset),
+    rssiOffset(wRssiOffset), sig_cbfn(NULL),
     mSPSTx(tx_sps), mSPSRx(rx_sps), mChans(chans), mEdge(false), mOn(false), mForceClockInterface(false),
     mTxFreq(0.0), mRxFreq(0.0), mTSC(0), mMaxExpectedDelayAB(0), mMaxExpectedDelayNB(0),
     mWriteBurstToDiskMask(0)
@@ -219,6 +223,17 @@
   return true;
 }
 
+void Transceiver::setSignalHandler(osmo_signal_cbfn cbfn)
+{
+  if (this->sig_cbfn)
+    osmo_signal_unregister_handler(SS_TRANSC, this->sig_cbfn, NULL);
+
+  if (cbfn) {
+    this->sig_cbfn = cbfn;
+    osmo_signal_register_handler(SS_TRANSC, this->sig_cbfn, NULL);
+  }
+}
+
 /*
  * Start the transceiver
  *
@@ -885,8 +900,12 @@
 
 void Transceiver::driveReceiveRadio()
 {
-  if (!mRadioInterface->driveReceiveRadio()) {
+  int rc = mRadioInterface->driveReceiveRadio();
+  if (rc == 0) {
     usleep(100000);
+  } else if (rc < 0) {
+    LOG(FATAL) << "radio Interface receive failed, requesting stop.";
+    osmo_signal_dispatch(SS_TRANSC, S_TRANSC_STOP_REQUIRED, this);
   } else if (mForceClockInterface || mTransmitDeadlineClock > mLastClockUpdateTime + GSM::Time(216,0)) {
     mForceClockInterface = false;
     writeClockInterface();