1 // fgcom.cxx -- FGCom: Voice communication
3 // Written by Clement de l'Hamaide, started May 2013.
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 // standard library includes
29 #include <simgear/compiler.h>
30 #include <simgear/sg_inlines.h>
31 #include <simgear/debug/logstream.hxx>
32 #include <simgear/math/sg_geodesy.hxx>
33 #include <simgear/timing/timestamp.hxx>
35 // flightgear includes
36 #include <Main/fg_props.hxx>
37 #include <Main/globals.hxx>
38 #include <ATC/CommStation.hxx>
39 #include <Airports/airport.hxx>
40 #include <Navaids/navlist.hxx>
42 #include <3rdparty/iaxclient/lib/iaxclient.h>
46 #define MAX_GND_RANGE 10.0
47 #define MAX_TWR_RANGE 50.0
48 #define MAX_RANGE 100.0
49 #define MIN_RANGE 20.0
50 #define MIN_GNDTWR_RANGE 0.0
51 #define DEFAULT_SERVER "fgcom.flightgear.org"
52 #define IAX_DELAY 300 // delay between calls in milliseconds
53 #define TEST_FREQ 910.00
54 #define NULL_ICAO "ZZZZ"
56 const int special_freq[] = { // Define some freq who need to be used with NULL_ICAO
67 static FGCom* static_instance = NULL;
71 static int iaxc_callback( iaxc_event e )
76 static_instance->iaxTextEvent(e.ev.text);
86 void FGCom::iaxTextEvent(struct iaxc_ev_text text)
88 if( (text.type == IAXC_TEXT_TYPE_STATUS ||
89 text.type == IAXC_TEXT_TYPE_IAX) &&
90 _showMessages_node->getBoolValue() )
92 _text_node->setStringValue(text.message);
101 _listener_active = 0;
114 SGPropertyNode *node = fgGetNode("/sim/fgcom", 0, true);
115 _test_node = node->getChild( "test", 0, true );
116 _server_node = node->getChild( "server", 0, true );
117 _enabled_node = node->getChild( "enabled", 0, true );
118 _micBoost_node = node->getChild( "mic-boost", 0, true );
119 _micLevel_node = node->getChild( "mic-level", 0, true );
120 _speakerLevel_node = node->getChild( "speaker-level", 0, true );
121 _selectedInput_node = node->getChild( "device-input", 0, true );
122 _selectedOutput_node = node->getChild( "device-output", 0, true );
123 _showMessages_node = node->getChild( "show-messages", 0, true );
125 SGPropertyNode *reg_node = node->getChild("register", 0, true);
126 _register_node = reg_node->getChild( "enabled", 0, true );
127 _username_node = reg_node->getChild( "username", 0, true );
128 _password_node = reg_node->getChild( "password", 0, true );
130 //_nav0_node = fgGetNode("/instrumentation/nav[0]/frequencies/selected-mhz", true);
131 //_nav1_node = fgGetNode("/instrumentation/nav[1]/frequencies/selected-mhz", true);
132 _comm0_node = fgGetNode("/instrumentation/comm[0]/frequencies/selected-mhz", true);
133 //_comm1_node = fgGetNode("/instrumentation/comm[1]/frequencies/selected-mhz", true);
134 _ptt0_node = fgGetNode("/instrumentation/comm[0]/ptt", true); //FIXME: what about /instrumentation/comm[1]/ptt ?
135 _callsign_node = fgGetNode("/sim/multiplay/callsign", true);
136 _text_node = fgGetNode("/sim/messages/atc", true );
137 _version_node = fgGetNode("/sim/version/flightgear", true );
139 // Set default values if not provided
140 if ( !_enabled_node->hasValue() )
141 _enabled_node->setBoolValue(true);
143 if ( !_test_node->hasValue() )
144 _test_node->setBoolValue(false);
146 if ( !_micBoost_node->hasValue() )
147 _micBoost_node->setIntValue(1);
149 if ( !_server_node->hasValue() )
150 _server_node->setStringValue(DEFAULT_SERVER);
152 if ( !_speakerLevel_node->hasValue() )
153 _speakerLevel_node->setFloatValue(1.0);
155 if ( !_micLevel_node->hasValue() )
156 _micLevel_node->setFloatValue(1.0);
158 if ( !_register_node->hasValue() )
159 _register_node->setBoolValue(false);
161 if ( !_username_node->hasValue() )
162 _username_node->setStringValue("guest");
164 if ( !_password_node->hasValue() )
165 _password_node->setStringValue("guest");
167 if ( !_showMessages_node->hasValue() )
168 _showMessages_node->setBoolValue(false);
170 _selectedOutput_node->addChangeListener(this);
171 _selectedInput_node->addChangeListener(this);
172 _speakerLevel_node->addChangeListener(this);
173 _micBoost_node->addChangeListener(this);
174 _micLevel_node->addChangeListener(this);
175 _enabled_node->addChangeListener(this);
176 _comm0_node->addChangeListener(this);
177 //_comm1_node->addChangeListener(this);
178 //_nav0_node->addChangeListener(this);
179 //_nav1_node->addChangeListener(this);
180 _ptt0_node->addChangeListener(this);
181 _test_node->addChangeListener(this);
194 _enabled = _enabled_node->getBoolValue();
195 _server = _server_node->getStringValue();
196 _register = _register_node->getBoolValue();
197 _username = _username_node->getStringValue();
198 _password = _password_node->getStringValue();
200 _currentComm0 = _comm0_node->getDoubleValue();
201 //_currentComm1 = _comm1_node->getDoubleValue();
202 //_currentNav0 = _nav0_node->getDoubleValue();
203 //_currentNav1 = _nav1_node->getDoubleValue();
205 _comm0Changed = false;
206 //_comm1Changed = false;
207 //_nav0Changed = false;
208 //_nav1Changed = false;
210 _maxRange = MAX_RANGE;
211 _minRange = MIN_RANGE;
216 void FGCom::postinit()
222 //WARNING: this _must_ be executed after sound system is totally initialized !
223 if( iaxc_initialize(NUM_CALLS) ) {
224 SG_LOG(SG_IO, SG_ALERT, "FGCom: cannot initialize iaxclient");
229 assert( static_instance == NULL );
230 static_instance = this;
231 iaxc_set_event_callback( iaxc_callback );
233 // FIXME: To be implemented in IAX audio driver
234 //iaxc_mic_boost_set( _micBoost_node->getIntValue() );
235 std::string app = "FGFS-";
236 app += _version_node->getStringValue();
238 iaxc_set_callerid( _callsign_node->getStringValue(), app.c_str() );
239 iaxc_set_formats( IAXC_FORMAT_GSM, IAXC_FORMAT_GSM );
240 iaxc_start_processing_thread ();
242 // Now IAXClient is initialized
246 _regId = iaxc_register( const_cast<char*>(_username.c_str()),
247 const_cast<char*>(_password.c_str()),
248 const_cast<char*>(_server.c_str()) );
250 SG_LOG(SG_IO, SG_ALERT, "FGCom: cannot register iaxclient");
256 Here we will create the list of available audio devices
257 Each audio device has a name, an ID, and a list of capabilities
258 If an audio device can output sound, available-output=true
259 If an audio device can input sound, available-input=true
261 /sim/fgcom/selected-input (int)
262 /sim/fgcom/selected-output (int)
264 /sim/fgcom/device[n]/id (int)
265 /sim/fgcom/device[n]/name (string)
266 /sim/fgcom/device[n]/available-input (bool)
267 /sim/fgcom/device[n]/available-output (bool)
270 //FIXME: OpenAL driver use an hard-coded device
271 // so all following is unused finally until someone
272 // implement "multi-device" support in IAX audio driver
273 SGPropertyNode *node = fgGetNode("/sim/fgcom", 0, true);
275 struct iaxc_audio_device *devs;
276 int nDevs, input, output, ring;
278 iaxc_audio_devices_get(&devs,&nDevs, &input, &output, &ring);
280 for(int i=0; i<nDevs; i++ ) {
281 SGPropertyNode *in_node = node->getChild("device", i, true);
284 _deviceID_node[i] = in_node->getChild("id", 0, true);
285 _deviceID_node[i]->setIntValue(devs[i].devID);
288 _deviceName_node[i] = in_node->getChild("name", 0, true);
289 _deviceName_node[i]->setStringValue(devs[i].name);
292 _deviceInput_node[i] = in_node->getChild("available-input", 0, true);
293 if( devs[i].capabilities & IAXC_AD_INPUT )
294 _deviceInput_node[i]->setBoolValue(true);
296 _deviceInput_node[i]->setBoolValue(false);
299 _deviceOutput_node[i] = in_node->getChild("available-output", 0, true);
300 if( devs[i].capabilities & IAXC_AD_OUTPUT )
301 _deviceOutput_node[i]->setBoolValue(true);
303 _deviceOutput_node[i]->setBoolValue(false);
305 // use default device at start
306 if( devs[i].capabilities & IAXC_AD_INPUT_DEFAULT )
307 _selectedInput_node->setIntValue(devs[i].devID);
308 if( devs[i].capabilities & IAXC_AD_OUTPUT_DEFAULT )
309 _selectedOutput_node->setIntValue(devs[i].devID);
312 // Mute the mic and set speaker at start
313 iaxc_input_level_set( 0.0 );
314 iaxc_output_level_set( _speakerLevel_node->getFloatValue() );
318 // Do the first call at start
319 const double freq = _comm0_node->getDoubleValue();
320 _currentFreqKhz = 10 * static_cast<int>(freq * 100 + 0.25);
321 std::string num = computePhoneNumber(freq, getAirportCode(freq));
323 _callComm0 = iaxc_call(num.c_str());
325 if( _callComm0 == -1 )
326 SG_LOG( SG_IO, SG_DEBUG, "FGCom: cannot call " << num.c_str() );
331 void FGCom::updateCall(bool& changed, int& callNo, double freqMHz)
334 _currentFreqKhz = 10 * static_cast<int>(freqMHz * 100 + 0.25);
337 if( !isInRange(freqMHz) ) {
338 iaxc_dump_call_number(callNo);
347 changed = false; // FIXME, out-params are confusing
350 iaxc_dump_call_number( callNo );
354 if(_p.elapsedMSec() > IAX_DELAY) {
355 std::string num = computePhoneNumber(freqMHz, getAirportCode(freqMHz));
356 if( !isInRange(freqMHz) )
359 callNo = iaxc_call(num.c_str());
362 SG_LOG( SG_IO, SG_DEBUG, "FGCom: cannot call " << num.c_str() );
371 void FGCom::update(double dt)
373 if ( !_enabled || !_initialized ) {
377 // For now we manage FGCom for only one freq because IAXClient
378 // is not able to handle multiple calls at same time.
379 updateCall(_comm0Changed, _callComm0, _comm0_node->getDoubleValue());
380 // updateCall(_comm1Changed, _callComm1, _comm1_node->getDoubleValue());
381 // updateCall(_nav0Changed, _callNav0, _nav0_node->getDoubleValue());
382 // updateCall(_nav1Changed, _callNav1, _nav1_node->getDoubleValue());
387 void FGCom::shutdown()
393 _initialized = false;
396 iaxc_set_event_callback(NULL);
397 iaxc_unregister(_regId);
398 iaxc_stop_processing_thread();
401 assert( static_instance == this );
402 static_instance = NULL;
407 void FGCom::valueChanged(SGPropertyNode *prop)
409 if (prop == _enabled_node) {
410 bool isEnabled = prop->getBoolValue();
411 if (_enabled == isEnabled) {
424 if (prop == _ptt0_node && _enabled) {
425 if( _ptt0_node->getBoolValue() ) {
426 iaxc_output_level_set( 0.0 );
427 iaxc_input_level_set( _micLevel_node->getFloatValue() ); //0.0 = min , 1.0 = max
429 iaxc_output_level_set( _speakerLevel_node->getFloatValue() );
430 iaxc_input_level_set( 0.0 );
434 if (prop == _test_node) {
435 testMode( prop->getBoolValue() );
439 //FIXME: not implemented in IAX audio driver (audio_openal.c)
440 if (prop == _micBoost_node && _initialized) {
441 int micBoost = prop->getIntValue();
442 SG_CLAMP_RANGE<int>( micBoost, 0, 1 );
443 iaxc_mic_boost_set( micBoost ) ; // 0 = enabled , 1 = disabled
447 //FIXME: not implemented in IAX audio driver (audio_openal.c)
448 if ((prop == _selectedInput_node || prop == _selectedOutput_node) && _initialized) {
449 int selectedInput = _selectedInput_node->getIntValue();
450 int selectedOutput = _selectedOutput_node->getIntValue();
451 iaxc_audio_devices_set(selectedInput, selectedOutput, 0);
455 if (_listener_active)
460 if (prop == _speakerLevel_node && _enabled) {
461 float speakerLevel = prop->getFloatValue();
462 SG_CLAMP_RANGE<float>( speakerLevel, 0.0, 1.0 );
463 _speakerLevel_node->setFloatValue(speakerLevel);
464 //iaxc_output_level_set(speakerLevel);
467 if (prop == _micLevel_node && _enabled) {
468 float micLevel = prop->getFloatValue();
469 SG_CLAMP_RANGE<float>( micLevel, 0.0, 1.0 );
470 _micLevel_node->setFloatValue(micLevel);
471 //iaxc_input_level_set(micLevel);
474 if (prop == _comm0_node) {
475 if( _currentComm0 != prop->getDoubleValue() ) {
476 _currentComm0 = prop->getDoubleValue();
478 _comm0Changed = true;
482 if (prop == _comm1_node) {
483 if( _currentComm1 != prop->getDoubleValue() ) {
484 _currentComm1 = prop->getDoubleValue();
486 _comm1Changed = true;
490 if (prop == _nav0_node) {
491 if( _currentNav0 != prop->getDoubleValue() ) {
492 _currentNav0 = prop->getDoubleValue();
497 if (prop == _nav1_node) {
498 if( _currentNav1 != prop->getDoubleValue() ) {
499 _currentNav1 = prop->getDoubleValue();
510 void FGCom::testMode(bool testMode)
512 if(testMode && _initialized) {
514 iaxc_dump_call_number(_callComm0);
515 iaxc_input_level_set( _micLevel_node->getFloatValue() );
516 iaxc_output_level_set( _speakerLevel_node->getFloatValue() );
517 std::string num = computePhoneNumber(TEST_FREQ, NULL_ICAO);
518 if( num.size() > 0 ) {
519 iaxc_millisleep(IAX_DELAY);
520 _callComm0 = iaxc_call(num.c_str());
522 if( _callComm0 == -1 )
523 SG_LOG( SG_IO, SG_DEBUG, "FGCom: cannot call " << num.c_str() );
526 iaxc_dump_call_number(_callComm0);
527 iaxc_millisleep(IAX_DELAY);
537 \param freq The requested frequency e.g 120.825
538 \return The ICAO code as string e.g LFMV
541 std::string FGCom::getAirportCode(const double& freq)
543 SGGeod aircraftPos = globals->get_aircraft_position();
545 for(size_t i=0; i<sizeof(special_freq)/sizeof(special_freq[0]); i++) { // Check if it's a special freq
546 if(special_freq[i] == _currentFreqKhz) {
551 flightgear::CommStation* apt = flightgear::CommStation::findByFreq(_currentFreqKhz, aircraftPos);
553 return std::string();
556 if( apt->type() == FGPositioned::FREQ_TOWER ) {
557 _maxRange = MAX_TWR_RANGE;
558 _minRange = MIN_GNDTWR_RANGE;
559 } else if( apt->type() == FGPositioned::FREQ_GROUND ) {
560 _maxRange = MAX_GND_RANGE;
561 _minRange = MIN_GNDTWR_RANGE;
563 _maxRange = MAX_RANGE;
564 _minRange = MIN_RANGE;
567 _aptPos = apt->geod();
568 return apt->airport()->ident();
574 \param freq The requested frequency e.g 112.7
575 \return The ICAO code as string e.g ITS
578 std::string FGCom::getVorCode(const double& freq) const
580 SGGeod aircraftPos = globals->get_aircraft_position();
581 FGNavList::TypeFilter filter(FGPositioned::VOR);
583 FGNavRecord* vor = FGNavList::findByFreq( freq, aircraftPos, &filter);
585 return std::string();
588 return vor->get_ident();
594 \param freq The requested frequency e.g 120.825
595 \param iaco The associated ICAO code e.g LFMV
596 \return The phone number as string i.e username:password@fgcom.flightgear.org/0176707786120825
599 std::string FGCom::computePhoneNumber(const double& freq, const std::string& icao) const
602 return std::string();
604 char phoneNumber[256];
608 /*Convert ICAO to ASCII */
609 sprintf( tmp, "%4s", icao.c_str() );
611 /*Built the phone number */
613 "%02d%02d%02d%02d%02d%06d",
619 (int) (freq * 1000 + 0.5) );
622 snprintf( phoneNumber,
623 sizeof (phoneNumber),
636 \return A boolean value, 1=in range, 0=out of range
639 bool FGCom::isInRange(const double &freq) const
641 for(size_t i=0; i<sizeof(special_freq)/sizeof(special_freq[0]); i++) { // Check if it's a special freq
642 if( (special_freq[i]) == _currentFreqKhz ) {
647 SGGeod acftPos = globals->get_aircraft_position();
648 double distNm = SGGeodesy::distanceNm(_aptPos, acftPos);
649 double delta_elevation_ft = fabs(acftPos.getElevationFt() - _aptPos.getElevationFt());
650 double rangeNm = 1.23 * sqrt(delta_elevation_ft);
652 if (rangeNm > _maxRange) rangeNm = _maxRange;
653 if (rangeNm < _minRange) rangeNm = _minRange;
654 if( distNm > rangeNm ) return 0;