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 <utils/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"
55 #define NUM_ID "0100000000000001"
57 const int special_freq[] = { // Define some freq who need to be used with NULL_ICAO
68 static FGCom* static_instance = NULL;
72 static int iaxc_callback( iaxc_event e )
77 static_instance->iaxTextEvent(e.ev.text);
87 void FGCom::iaxTextEvent(struct iaxc_ev_text text)
89 if( (text.type == IAXC_TEXT_TYPE_STATUS ||
90 text.type == IAXC_TEXT_TYPE_IAX) &&
91 _showMessages_node->getBoolValue() )
93 _text_node->setStringValue(text.message);
102 _listener_active = 0;
115 SGPropertyNode *node = fgGetNode("/sim/fgcom", 0, true);
116 _test_node = node->getChild( "test", 0, true );
117 _server_node = node->getChild( "server", 0, true );
118 _enabled_node = node->getChild( "enabled", 0, true );
119 _micBoost_node = node->getChild( "mic-boost", 0, true );
120 _micLevel_node = node->getChild( "mic-level", 0, true );
121 _speakerLevel_node = node->getChild( "speaker-level", 0, true );
122 _selectedInput_node = node->getChild( "device-input", 0, true );
123 _selectedOutput_node = node->getChild( "device-output", 0, true );
124 _showMessages_node = node->getChild( "show-messages", 0, true );
126 SGPropertyNode *reg_node = node->getChild("register", 0, true);
127 _register_node = reg_node->getChild( "enabled", 0, true );
128 _username_node = reg_node->getChild( "username", 0, true );
129 _password_node = reg_node->getChild( "password", 0, true );
131 //_nav0_node = fgGetNode("/instrumentation/nav[0]/frequencies/selected-mhz", true);
132 //_nav1_node = fgGetNode("/instrumentation/nav[1]/frequencies/selected-mhz", true);
133 _comm0_node = fgGetNode("/instrumentation/comm[0]/frequencies/selected-mhz", true);
134 //_comm1_node = fgGetNode("/instrumentation/comm[1]/frequencies/selected-mhz", true);
135 _ptt0_node = fgGetNode("/instrumentation/comm[0]/ptt", true); //FIXME: what about /instrumentation/comm[1]/ptt ?
136 _callsign_node = fgGetNode("/sim/multiplay/callsign", true);
137 _text_node = fgGetNode("/sim/messages/atc", true );
138 _version_node = fgGetNode("/sim/version/flightgear", true );
140 // Set default values if not provided
141 if ( !_enabled_node->hasValue() )
142 _enabled_node->setBoolValue(true);
144 if ( !_test_node->hasValue() )
145 _test_node->setBoolValue(false);
147 if ( !_micBoost_node->hasValue() )
148 _micBoost_node->setIntValue(1);
150 if ( !_server_node->hasValue() )
151 _server_node->setStringValue(DEFAULT_SERVER);
153 if ( !_speakerLevel_node->hasValue() )
154 _speakerLevel_node->setFloatValue(1.0);
156 if ( !_micLevel_node->hasValue() )
157 _micLevel_node->setFloatValue(1.0);
159 if ( !_register_node->hasValue() )
160 _register_node->setBoolValue(false);
162 if ( !_username_node->hasValue() )
163 _username_node->setStringValue("guest");
165 if ( !_password_node->hasValue() )
166 _password_node->setStringValue("guest");
168 if ( !_showMessages_node->hasValue() )
169 _showMessages_node->setBoolValue(false);
171 _selectedOutput_node->addChangeListener(this);
172 _selectedInput_node->addChangeListener(this);
173 _speakerLevel_node->addChangeListener(this);
174 _micBoost_node->addChangeListener(this);
175 _micLevel_node->addChangeListener(this);
176 _enabled_node->addChangeListener(this);
177 _comm0_node->addChangeListener(this);
178 //_comm1_node->addChangeListener(this);
179 //_nav0_node->addChangeListener(this);
180 //_nav1_node->addChangeListener(this);
181 _ptt0_node->addChangeListener(this);
182 _test_node->addChangeListener(this);
195 _enabled = _enabled_node->getBoolValue();
196 _server = _server_node->getStringValue();
197 _register = _register_node->getBoolValue();
198 _username = _username_node->getStringValue();
199 _password = _password_node->getStringValue();
201 _currentComm0 = _comm0_node->getDoubleValue();
202 //_currentComm1 = _comm1_node->getDoubleValue();
203 //_currentNav0 = _nav0_node->getDoubleValue();
204 //_currentNav1 = _nav1_node->getDoubleValue();
206 _comm0Changed = false;
207 //_comm1Changed = false;
208 //_nav0Changed = false;
209 //_nav1Changed = false;
211 _maxRange = MAX_RANGE;
212 _minRange = MIN_RANGE;
217 void FGCom::postinit()
223 //WARNING: this _must_ be executed after sound system is totally initialized !
224 if( iaxc_initialize(NUM_CALLS) ) {
225 SG_LOG(SG_IO, SG_ALERT, "FGCom: cannot initialize iaxclient");
230 assert( static_instance == NULL );
231 static_instance = this;
232 iaxc_set_event_callback( iaxc_callback );
234 // FIXME: To be implemented in IAX audio driver
235 //iaxc_mic_boost_set( _micBoost_node->getIntValue() );
236 std::string app = "FGFS-";
237 app += _version_node->getStringValue();
239 iaxc_set_callerid( _callsign_node->getStringValue(), app.c_str() );
240 iaxc_set_formats( IAXC_FORMAT_GSM, IAXC_FORMAT_GSM );
241 iaxc_start_processing_thread ();
243 // Now IAXClient is initialized
247 _regId = iaxc_register( const_cast<char*>(_username.c_str()),
248 const_cast<char*>(_password.c_str()),
249 const_cast<char*>(_server.c_str()) );
251 SG_LOG(SG_IO, SG_ALERT, "FGCom: cannot register iaxclient");
257 Here we will create the list of available audio devices
258 Each audio device has a name, an ID, and a list of capabilities
259 If an audio device can output sound, available-output=true
260 If an audio device can input sound, available-input=true
262 /sim/fgcom/selected-input (int)
263 /sim/fgcom/selected-output (int)
265 /sim/fgcom/device[n]/id (int)
266 /sim/fgcom/device[n]/name (string)
267 /sim/fgcom/device[n]/available-input (bool)
268 /sim/fgcom/device[n]/available-output (bool)
271 //FIXME: OpenAL driver use an hard-coded device
272 // so all following is unused finally until someone
273 // implement "multi-device" support in IAX audio driver
274 SGPropertyNode *node = fgGetNode("/sim/fgcom", 0, true);
276 struct iaxc_audio_device *devs;
277 int nDevs, input, output, ring;
279 iaxc_audio_devices_get(&devs,&nDevs, &input, &output, &ring);
281 for(int i=0; i<nDevs; i++ ) {
282 SGPropertyNode *in_node = node->getChild("device", i, true);
285 _deviceID_node[i] = in_node->getChild("id", 0, true);
286 _deviceID_node[i]->setIntValue(devs[i].devID);
289 _deviceName_node[i] = in_node->getChild("name", 0, true);
290 _deviceName_node[i]->setStringValue(devs[i].name);
293 _deviceInput_node[i] = in_node->getChild("available-input", 0, true);
294 if( devs[i].capabilities & IAXC_AD_INPUT )
295 _deviceInput_node[i]->setBoolValue(true);
297 _deviceInput_node[i]->setBoolValue(false);
300 _deviceOutput_node[i] = in_node->getChild("available-output", 0, true);
301 if( devs[i].capabilities & IAXC_AD_OUTPUT )
302 _deviceOutput_node[i]->setBoolValue(true);
304 _deviceOutput_node[i]->setBoolValue(false);
306 // use default device at start
307 if( devs[i].capabilities & IAXC_AD_INPUT_DEFAULT )
308 _selectedInput_node->setIntValue(devs[i].devID);
309 if( devs[i].capabilities & IAXC_AD_OUTPUT_DEFAULT )
310 _selectedOutput_node->setIntValue(devs[i].devID);
313 // Mute the mic and set speaker at start
314 iaxc_input_level_set( 0.0 );
315 iaxc_output_level_set( _speakerLevel_node->getFloatValue() );
319 // Do the first call at start
320 const double freq = _comm0_node->getDoubleValue();
321 _currentFreqKhz = 10 * static_cast<int>(freq * 100 + 0.25);
322 std::string num = computePhoneNumber(freq, getAirportCode(freq));
324 _callComm0 = iaxc_call(num.c_str());
326 if( _callComm0 == -1 )
327 SG_LOG( SG_IO, SG_DEBUG, "FGCom: cannot call " << num.c_str() );
332 void FGCom::updateCall(bool& changed, int& callNo, double freqMHz)
335 _currentFreqKhz = 10 * static_cast<int>(freqMHz * 100 + 0.25);
338 if( !isInRange(freqMHz) ) {
339 iaxc_dump_call_number(callNo);
348 changed = false; // FIXME, out-params are confusing
351 iaxc_dump_call_number( callNo );
355 if(_p.elapsedMSec() > IAX_DELAY) {
356 std::string num = computePhoneNumber(freqMHz, getAirportCode(freqMHz));
357 if( !isInRange(freqMHz) )
360 callNo = iaxc_call(num.c_str());
363 SG_LOG( SG_IO, SG_DEBUG, "FGCom: cannot call " << num.c_str() );
372 void FGCom::update(double dt)
374 if ( !_enabled || !_initialized ) {
378 // For now we manage FGCom for only one freq because IAXClient
379 // is not able to handle multiple calls at same time.
380 updateCall(_comm0Changed, _callComm0, _comm0_node->getDoubleValue());
381 // updateCall(_comm1Changed, _callComm1, _comm1_node->getDoubleValue());
382 // updateCall(_nav0Changed, _callNav0, _nav0_node->getDoubleValue());
383 // updateCall(_nav1Changed, _callNav1, _nav1_node->getDoubleValue());
388 void FGCom::shutdown()
394 _initialized = false;
397 iaxc_set_event_callback(NULL);
398 iaxc_unregister(_regId);
399 iaxc_stop_processing_thread();
402 assert( static_instance == this );
403 static_instance = NULL;
408 void FGCom::valueChanged(SGPropertyNode *prop)
410 if (prop == _enabled_node) {
411 bool isEnabled = prop->getBoolValue();
412 if (_enabled == isEnabled) {
425 if (prop == _ptt0_node && _enabled) {
426 if( _ptt0_node->getBoolValue() ) {
427 iaxc_output_level_set( 0.0 );
428 iaxc_input_level_set( _micLevel_node->getFloatValue() ); //0.0 = min , 1.0 = max
430 iaxc_output_level_set( _speakerLevel_node->getFloatValue() );
431 iaxc_input_level_set( 0.0 );
435 if (prop == _test_node) {
436 testMode( prop->getBoolValue() );
440 //FIXME: not implemented in IAX audio driver (audio_openal.c)
441 if (prop == _micBoost_node && _initialized) {
442 int micBoost = prop->getIntValue();
443 SG_CLAMP_RANGE<int>( micBoost, 0, 1 );
444 iaxc_mic_boost_set( micBoost ) ; // 0 = enabled , 1 = disabled
448 //FIXME: not implemented in IAX audio driver (audio_openal.c)
449 if ((prop == _selectedInput_node || prop == _selectedOutput_node) && _initialized) {
450 int selectedInput = _selectedInput_node->getIntValue();
451 int selectedOutput = _selectedOutput_node->getIntValue();
452 iaxc_audio_devices_set(selectedInput, selectedOutput, 0);
456 if (_listener_active)
461 if (prop == _speakerLevel_node && _enabled) {
462 float speakerLevel = prop->getFloatValue();
463 SG_CLAMP_RANGE<float>( speakerLevel, 0.0, 1.0 );
464 _speakerLevel_node->setFloatValue(speakerLevel);
465 //iaxc_output_level_set(speakerLevel);
468 if (prop == _micLevel_node && _enabled) {
469 float micLevel = prop->getFloatValue();
470 SG_CLAMP_RANGE<float>( micLevel, 0.0, 1.0 );
471 _micLevel_node->setFloatValue(micLevel);
472 //iaxc_input_level_set(micLevel);
475 if (prop == _comm0_node) {
476 if( _currentComm0 != prop->getDoubleValue() ) {
477 _currentComm0 = prop->getDoubleValue();
479 _comm0Changed = true;
483 if (prop == _comm1_node) {
484 if( _currentComm1 != prop->getDoubleValue() ) {
485 _currentComm1 = prop->getDoubleValue();
487 _comm1Changed = true;
491 if (prop == _nav0_node) {
492 if( _currentNav0 != prop->getDoubleValue() ) {
493 _currentNav0 = prop->getDoubleValue();
498 if (prop == _nav1_node) {
499 if( _currentNav1 != prop->getDoubleValue() ) {
500 _currentNav1 = prop->getDoubleValue();
511 void FGCom::testMode(bool testMode)
513 if(testMode && _initialized) {
515 iaxc_dump_call_number(_callComm0);
516 iaxc_input_level_set( _micLevel_node->getFloatValue() );
517 iaxc_output_level_set( _speakerLevel_node->getFloatValue() );
518 std::string num = computePhoneNumber(TEST_FREQ, NULL_ICAO);
519 if( num.size() > 0 ) {
520 iaxc_millisleep(IAX_DELAY);
521 _callComm0 = iaxc_call(num.c_str());
523 if( _callComm0 == -1 )
524 SG_LOG( SG_IO, SG_DEBUG, "FGCom: cannot call " << num.c_str() );
527 iaxc_dump_call_number(_callComm0);
528 iaxc_millisleep(IAX_DELAY);
538 \param freq The requested frequency e.g 120.825
539 \return The ICAO code as string e.g LFMV
542 std::string FGCom::getAirportCode(const double& freq)
544 SGGeod aircraftPos = globals->get_aircraft_position();
546 for(size_t i=0; i<sizeof(special_freq)/sizeof(special_freq[0]); i++) { // Check if it's a special freq
547 if(special_freq[i] == _currentFreqKhz) {
552 flightgear::CommStation* apt = flightgear::CommStation::findByFreq(_currentFreqKhz, aircraftPos);
554 return std::string();
557 if( apt->type() == FGPositioned::FREQ_TOWER ) {
558 _maxRange = MAX_TWR_RANGE;
559 _minRange = MIN_GNDTWR_RANGE;
560 } else if( apt->type() == FGPositioned::FREQ_GROUND ) {
561 _maxRange = MAX_GND_RANGE;
562 _minRange = MIN_GNDTWR_RANGE;
564 _maxRange = MAX_RANGE;
565 _minRange = MIN_RANGE;
568 _aptPos = apt->geod();
569 return apt->airport()->ident();
575 \param freq The requested frequency e.g 112.7
576 \return The ICAO code as string e.g ITS
579 std::string FGCom::getVorCode(const double& freq) const
581 SGGeod aircraftPos = globals->get_aircraft_position();
582 FGNavList::TypeFilter filter(FGPositioned::VOR);
584 FGNavRecord* vor = FGNavList::findByFreq( freq, aircraftPos, &filter);
586 return std::string();
589 return vor->get_ident();
595 \param freq The requested frequency e.g 120.825
596 \param iaco The associated ICAO code e.g LFMV
597 \return The phone number as string i.e username:password@fgcom.flightgear.org/0176707786120825
600 std::string FGCom::computePhoneNumber(const double& freq, const std::string& icao) const
603 return std::string();
605 char phoneNumber[256];
609 /*Convert ICAO to ASCII */
610 sprintf( tmp, "%4s", icao.c_str() );
612 /*Built the phone number */
614 "%02d%02d%02d%02d%02d%06d",
620 (int) (freq * 1000 + 0.5) );
623 snprintf( phoneNumber,
624 sizeof (phoneNumber),
637 \return A boolean value, 1=in range, 0=out of range
640 bool FGCom::isInRange(const double &freq) const
642 for(size_t i=0; i<sizeof(special_freq)/sizeof(special_freq[0]); i++) { // Check if it's a special freq
643 if( (special_freq[i]) == _currentFreqKhz ) {
648 SGGeod acftPos = globals->get_aircraft_position();
649 double distNm = SGGeodesy::distanceNm(_aptPos, acftPos);
650 double delta_elevation_ft = fabs(acftPos.getElevationFt() - _aptPos.getElevationFt());
651 double rangeNm = 1.23 * sqrt(delta_elevation_ft);
653 if (rangeNm > _maxRange) rangeNm = _maxRange;
654 if (rangeNm < _minRange) rangeNm = _minRange;
655 if( distNm > rangeNm ) return 0;