]> git.mxchange.org Git - flightgear.git/blobdiff - src/Network/fgcom.cxx
Fix for bug 1304 - crash loading XML route
[flightgear.git] / src / Network / fgcom.cxx
index adc541a7aa98e1594a756f037dc19db6897573d2..8d1bfc96c8bb332af58b23ac6677891247fb75f3 100644 (file)
@@ -39,7 +39,7 @@
 #include <Airports/airport.hxx>
 #include <Navaids/navlist.hxx>
 
-#include <utils/iaxclient/lib/iaxclient.h>
+#include <3rdparty/iaxclient/lib/iaxclient.h>
 
 
 #define NUM_CALLS 4
@@ -52,7 +52,6 @@
 #define IAX_DELAY 300  // delay between calls in milliseconds
 #define TEST_FREQ 910.00
 #define NULL_ICAO "ZZZZ"
-#define NUM_ID "0100000000000001"
 
 const int special_freq[] = { // Define some freq who need to be used with NULL_ICAO
        910000,
@@ -61,9 +60,7 @@ const int special_freq[] = { // Define some freq who need to be used with NULL_I
        123450,
        122750,
        121500,
-       123500,
-       121000,
-       723340 };
+       123500 };
 
 static FGCom* static_instance = NULL;
 
@@ -97,9 +94,11 @@ void FGCom::iaxTextEvent(struct iaxc_ev_text text)
 
 
 FGCom::FGCom() :
-    _register(true)
+    _register(true),
+    _enabled(false),
+    _initialized(false),
+    _listener_active(0)
 {
-  _listener_active = 0;
 }
 
 
@@ -118,6 +117,7 @@ void FGCom::bind()
   _enabled_node            = node->getChild( "enabled", 0, true );
   _micBoost_node           = node->getChild( "mic-boost", 0, true );
   _micLevel_node           = node->getChild( "mic-level", 0, true );
+  _silenceThd_node         = node->getChild( "silence-threshold", 0, true );
   _speakerLevel_node       = node->getChild( "speaker-level", 0, true );
   _selectedInput_node      = node->getChild( "device-input", 0, true );
   _selectedOutput_node     = node->getChild( "device-output", 0, true );
@@ -128,10 +128,8 @@ void FGCom::bind()
   _username_node           = reg_node->getChild( "username", 0, true );
   _password_node           = reg_node->getChild( "password", 0, true );
 
-  //_nav0_node               = fgGetNode("/instrumentation/nav[0]/frequencies/selected-mhz", true);
-  //_nav1_node               = fgGetNode("/instrumentation/nav[1]/frequencies/selected-mhz", true);
   _comm0_node              = fgGetNode("/instrumentation/comm[0]/frequencies/selected-mhz", true);
-  //_comm1_node              = fgGetNode("/instrumentation/comm[1]/frequencies/selected-mhz", true);
+  _comm1_node              = fgGetNode("/instrumentation/comm[1]/frequencies/selected-mhz", true);
   _ptt0_node               = fgGetNode("/instrumentation/comm[0]/ptt", true); //FIXME: what about /instrumentation/comm[1]/ptt ?
   _callsign_node           = fgGetNode("/sim/multiplay/callsign", true);
   _text_node               = fgGetNode("/sim/messages/atc", true );
@@ -156,6 +154,9 @@ void FGCom::bind()
   if ( !_micLevel_node->hasValue() )
       _micLevel_node->setFloatValue(1.0);
 
+  if ( !_silenceThd_node->hasValue() )
+      _silenceThd_node->setFloatValue(-35.0);
+
   if ( !_register_node->hasValue() )
       _register_node->setBoolValue(false);
 
@@ -171,13 +172,12 @@ void FGCom::bind()
   _selectedOutput_node->addChangeListener(this);
   _selectedInput_node->addChangeListener(this);
   _speakerLevel_node->addChangeListener(this);
+  _silenceThd_node->addChangeListener(this);
   _micBoost_node->addChangeListener(this);
   _micLevel_node->addChangeListener(this);
   _enabled_node->addChangeListener(this);
   _comm0_node->addChangeListener(this);
-  //_comm1_node->addChangeListener(this);
-  //_nav0_node->addChangeListener(this);
-  //_nav1_node->addChangeListener(this);
+  _comm1_node->addChangeListener(this);
   _ptt0_node->addChangeListener(this);
   _test_node->addChangeListener(this);
 }
@@ -197,16 +197,13 @@ void FGCom::init()
   _register         = _register_node->getBoolValue();
   _username         = _username_node->getStringValue();
   _password         = _password_node->getStringValue();
+  _selectedComm     = 0;
 
   _currentComm0     = _comm0_node->getDoubleValue();
-  //_currentComm1     = _comm1_node->getDoubleValue();
-  //_currentNav0      = _nav0_node->getDoubleValue();
-  //_currentNav1      = _nav1_node->getDoubleValue();
+  _currentComm1     = _comm1_node->getDoubleValue();
 
   _comm0Changed     = false;
-  //_comm1Changed     = false;
-  //_nav0Changed      = false;
-  //_nav1Changed      = false;
+  _comm1Changed     = false;
 
   _maxRange         = MAX_RANGE;
   _minRange         = MIN_RANGE;
@@ -237,7 +234,10 @@ void FGCom::postinit()
     app += _version_node->getStringValue();
 
     iaxc_set_callerid( _callsign_node->getStringValue(), app.c_str() );
-    iaxc_set_formats( IAXC_FORMAT_GSM, IAXC_FORMAT_GSM );
+    iaxc_set_formats (IAXC_FORMAT_SPEEX, IAXC_FORMAT_ULAW|IAXC_FORMAT_SPEEX);
+    iaxc_set_speex_settings(1, 5, 0, 1, 0, 3);
+    iaxc_set_filters(IAXC_FILTER_AGC | IAXC_FILTER_DENOISE);
+    iaxc_set_silence_threshold(_silenceThd_node->getFloatValue());
     iaxc_start_processing_thread ();
 
     // Now IAXClient is initialized
@@ -377,10 +377,11 @@ void FGCom::update(double dt)
 
     // For now we manage FGCom for only one freq because IAXClient
     // is not able to handle multiple calls at same time.
-    updateCall(_comm0Changed, _callComm0, _comm0_node->getDoubleValue());
-    // updateCall(_comm1Changed, _callComm1, _comm1_node->getDoubleValue());
-    // updateCall(_nav0Changed, _callNav0, _nav0_node->getDoubleValue());
-    // updateCall(_nav1Changed, _callNav1, _nav1_node->getDoubleValue());
+    if( _selectedComm == 0) {
+        updateCall(_comm0Changed, _callComm0, _comm0_node->getDoubleValue());
+    } else {
+        updateCall(_comm1Changed, _callComm0, _comm1_node->getDoubleValue());
+    }
 }
 
 
@@ -423,6 +424,18 @@ void FGCom::valueChanged(SGPropertyNode *prop)
   }
 
   if (prop == _ptt0_node && _enabled) {
+    if( _ptt0_node->getIntValue() == 2 ) {
+      if( _selectedComm == 0 ) {
+          SG_LOG( SG_IO, SG_INFO, "FGCom: change comm source to comm[1]" );
+          _comm1Changed = true;
+          _selectedComm = 1;
+      } else {
+          SG_LOG( SG_IO, SG_INFO, "FGCom: change comm source to comm[0]" );
+          _comm0Changed = true;
+          _selectedComm = 0;
+      }
+      return;
+    }
     if( _ptt0_node->getBoolValue() ) {
       iaxc_output_level_set( 0.0 );
       iaxc_input_level_set( _micLevel_node->getFloatValue() ); //0.0 = min , 1.0 = max
@@ -437,6 +450,13 @@ void FGCom::valueChanged(SGPropertyNode *prop)
     return;
   }
 
+  if (prop == _silenceThd_node && _initialized) {
+    float silenceThd = prop->getFloatValue();
+    SG_CLAMP_RANGE<float>( silenceThd, -60, 0 );
+    iaxc_set_silence_threshold( silenceThd );
+    return;
+  }
+
   //FIXME: not implemented in IAX audio driver (audio_openal.c)
   if (prop == _micBoost_node && _initialized) {
     int micBoost = prop->getIntValue();
@@ -462,7 +482,7 @@ void FGCom::valueChanged(SGPropertyNode *prop)
     float speakerLevel = prop->getFloatValue();
     SG_CLAMP_RANGE<float>( speakerLevel, 0.0, 1.0 );
     _speakerLevel_node->setFloatValue(speakerLevel);
-    //iaxc_output_level_set(speakerLevel);
+    iaxc_output_level_set(speakerLevel);
   }
 
   if (prop == _micLevel_node && _enabled) {
@@ -479,7 +499,7 @@ void FGCom::valueChanged(SGPropertyNode *prop)
       _comm0Changed = true;
     }
   }
-/*
+
   if (prop == _comm1_node) {
     if( _currentComm1 != prop->getDoubleValue() ) {
       _currentComm1 = prop->getDoubleValue();
@@ -488,21 +508,6 @@ void FGCom::valueChanged(SGPropertyNode *prop)
     }
   }
 
-  if (prop == _nav0_node) {
-    if( _currentNav0 != prop->getDoubleValue() ) {
-      _currentNav0 = prop->getDoubleValue();
-      _nav0Changed = true;
-    }
-  }
-
-  if (prop == _nav1_node) {
-    if( _currentNav1 != prop->getDoubleValue() ) {
-      _currentNav1 = prop->getDoubleValue();
-      _nav1Changed = true;
-    }
-  }
-*/
-
   _listener_active--;
 }
 
@@ -512,8 +517,8 @@ void FGCom::testMode(bool testMode)
 {
   if(testMode && _initialized) {
     _enabled = false;
-    iaxc_dump_call_number(_callComm0);
-    iaxc_input_level_set( _micLevel_node->getFloatValue() );
+    iaxc_dump_all_calls();
+    iaxc_input_level_set( 1.0 );
     iaxc_output_level_set( _speakerLevel_node->getFloatValue() );
     std::string num = computePhoneNumber(TEST_FREQ, NULL_ICAO);
     if( num.size() > 0 ) {
@@ -524,8 +529,10 @@ void FGCom::testMode(bool testMode)
       SG_LOG( SG_IO, SG_DEBUG, "FGCom: cannot call " << num.c_str() );
   } else {
     if( _initialized ) {
-      iaxc_dump_call_number(_callComm0);
+      iaxc_dump_all_calls();
       iaxc_millisleep(IAX_DELAY);
+      iaxc_input_level_set( 0.0 );
+      iaxc_output_level_set( _speakerLevel_node->getFloatValue() );
       _callComm0 = -1;
       _enabled = true;
     }
@@ -551,7 +558,10 @@ std::string FGCom::getAirportCode(const double& freq)
 
   flightgear::CommStation* apt = flightgear::CommStation::findByFreq(_currentFreqKhz, aircraftPos);
   if( !apt ) {
-    return std::string();
+    apt = flightgear::CommStation::findByFreq(_currentFreqKhz-10, aircraftPos); // Check for 8.33KHz
+    if( !apt ) {
+      return std::string();
+    }
   }
 
   if( apt->type() == FGPositioned::FREQ_TOWER ) {
@@ -571,26 +581,6 @@ std::string FGCom::getAirportCode(const double& freq)
 
 
 
-/*
-  \param freq The requested frequency e.g 112.7
-  \return The ICAO code as string e.g ITS
-*/
-/*
-std::string FGCom::getVorCode(const double& freq) const
-{
-  SGGeod aircraftPos = globals->get_aircraft_position();
-  FGNavList::TypeFilter filter(FGPositioned::VOR);
-
-  FGNavRecord* vor = FGNavList::findByFreq( freq, aircraftPos, &filter);
-  if( !vor ) {
-    return std::string();
-  }
-
-  return vor->get_ident();
-}
-*/
-
-
 /*
   \param freq The requested frequency e.g 120.825
   \param iaco The associated ICAO code e.g LFMV