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);
116 SGPropertyNode *node = fgGetNode("/sim/fgcom", 0, true);
117 _test_node = node->getChild( "test", 0, true );
118 _server_node = node->getChild( "server", 0, true );
119 _enabled_node = node->getChild( "enabled", 0, true );
120 _micBoost_node = node->getChild( "mic-boost", 0, true );
121 _micLevel_node = node->getChild( "mic-level", 0, true );
122 _speakerLevel_node = node->getChild( "speaker-level", 0, true );
123 _selectedInput_node = node->getChild( "device-input", 0, true );
124 _selectedOutput_node = node->getChild( "device-output", 0, true );
125 _showMessages_node = node->getChild( "show-messages", 0, true );
127 SGPropertyNode *reg_node = node->getChild("register", 0, true);
128 _register_node = reg_node->getChild( "enabled", 0, true );
129 _username_node = reg_node->getChild( "username", 0, true );
130 _password_node = reg_node->getChild( "password", 0, true );
132 //_nav0_node = fgGetNode("/instrumentation/nav[0]/frequencies/selected-mhz", true);
133 //_nav1_node = fgGetNode("/instrumentation/nav[1]/frequencies/selected-mhz", true);
134 _comm0_node = fgGetNode("/instrumentation/comm[0]/frequencies/selected-mhz", true);
135 //_comm1_node = fgGetNode("/instrumentation/comm[1]/frequencies/selected-mhz", true);
136 _ptt0_node = fgGetNode("/instrumentation/comm[0]/ptt", true); //FIXME: what about /instrumentation/comm[1]/ptt ?
137 _callsign_node = fgGetNode("/sim/multiplay/callsign", true);
138 _text_node = fgGetNode("/sim/messages/atc", true );
139 _version_node = fgGetNode("/sim/version/flightgear", true );
141 // Set default values if not provided
142 if ( !_enabled_node->hasValue() )
143 _enabled_node->setBoolValue(true);
145 if ( !_test_node->hasValue() )
146 _test_node->setBoolValue(false);
148 if ( !_micBoost_node->hasValue() )
149 _micBoost_node->setIntValue(1);
151 if ( !_server_node->hasValue() )
152 _server_node->setStringValue(DEFAULT_SERVER);
154 if ( !_speakerLevel_node->hasValue() )
155 _speakerLevel_node->setFloatValue(1.0);
157 if ( !_micLevel_node->hasValue() )
158 _micLevel_node->setFloatValue(1.0);
160 if ( !_register_node->hasValue() )
161 _register_node->setBoolValue(false);
163 if ( !_username_node->hasValue() )
164 _username_node->setStringValue("guest");
166 if ( !_password_node->hasValue() )
167 _password_node->setStringValue("guest");
169 if ( !_showMessages_node->hasValue() )
170 _showMessages_node->setBoolValue(false);
172 _selectedOutput_node->addChangeListener(this);
173 _selectedInput_node->addChangeListener(this);
174 _speakerLevel_node->addChangeListener(this);
175 _micBoost_node->addChangeListener(this);
176 _micLevel_node->addChangeListener(this);
177 _enabled_node->addChangeListener(this);
178 _comm0_node->addChangeListener(this);
179 //_comm1_node->addChangeListener(this);
180 //_nav0_node->addChangeListener(this);
181 //_nav1_node->addChangeListener(this);
182 _ptt0_node->addChangeListener(this);
183 _test_node->addChangeListener(this);
196 _enabled = _enabled_node->getBoolValue();
197 _server = _server_node->getStringValue();
198 _register = _register_node->getBoolValue();
199 _username = _username_node->getStringValue();
200 _password = _password_node->getStringValue();
202 _currentComm0 = _comm0_node->getDoubleValue();
203 //_currentComm1 = _comm1_node->getDoubleValue();
204 //_currentNav0 = _nav0_node->getDoubleValue();
205 //_currentNav1 = _nav1_node->getDoubleValue();
207 _comm0Changed = false;
208 //_comm1Changed = false;
209 //_nav0Changed = false;
210 //_nav1Changed = false;
212 _maxRange = MAX_RANGE;
213 _minRange = MIN_RANGE;
218 void FGCom::postinit()
224 //WARNING: this _must_ be executed after sound system is totally initialized !
225 if( iaxc_initialize(NUM_CALLS) ) {
226 SG_LOG(SG_IO, SG_ALERT, "FGCom: cannot initialize iaxclient");
231 assert( static_instance == NULL );
232 static_instance = this;
233 iaxc_set_event_callback( iaxc_callback );
235 // FIXME: To be implemented in IAX audio driver
236 //iaxc_mic_boost_set( _micBoost_node->getIntValue() );
237 std::string app = "FGFS-";
238 app += _version_node->getStringValue();
240 iaxc_set_callerid( _callsign_node->getStringValue(), app.c_str() );
241 iaxc_set_formats (IAXC_FORMAT_SPEEX, IAXC_FORMAT_SPEEX);
242 iaxc_set_speex_settings(1, 5, 0, 1, 0, 3);
243 iaxc_start_processing_thread ();
245 // Now IAXClient is initialized
249 _regId = iaxc_register( const_cast<char*>(_username.c_str()),
250 const_cast<char*>(_password.c_str()),
251 const_cast<char*>(_server.c_str()) );
253 SG_LOG(SG_IO, SG_ALERT, "FGCom: cannot register iaxclient");
259 Here we will create the list of available audio devices
260 Each audio device has a name, an ID, and a list of capabilities
261 If an audio device can output sound, available-output=true
262 If an audio device can input sound, available-input=true
264 /sim/fgcom/selected-input (int)
265 /sim/fgcom/selected-output (int)
267 /sim/fgcom/device[n]/id (int)
268 /sim/fgcom/device[n]/name (string)
269 /sim/fgcom/device[n]/available-input (bool)
270 /sim/fgcom/device[n]/available-output (bool)
273 //FIXME: OpenAL driver use an hard-coded device
274 // so all following is unused finally until someone
275 // implement "multi-device" support in IAX audio driver
276 SGPropertyNode *node = fgGetNode("/sim/fgcom", 0, true);
278 struct iaxc_audio_device *devs;
279 int nDevs, input, output, ring;
281 iaxc_audio_devices_get(&devs,&nDevs, &input, &output, &ring);
283 for(int i=0; i<nDevs; i++ ) {
284 SGPropertyNode *in_node = node->getChild("device", i, true);
287 _deviceID_node[i] = in_node->getChild("id", 0, true);
288 _deviceID_node[i]->setIntValue(devs[i].devID);
291 _deviceName_node[i] = in_node->getChild("name", 0, true);
292 _deviceName_node[i]->setStringValue(devs[i].name);
295 _deviceInput_node[i] = in_node->getChild("available-input", 0, true);
296 if( devs[i].capabilities & IAXC_AD_INPUT )
297 _deviceInput_node[i]->setBoolValue(true);
299 _deviceInput_node[i]->setBoolValue(false);
302 _deviceOutput_node[i] = in_node->getChild("available-output", 0, true);
303 if( devs[i].capabilities & IAXC_AD_OUTPUT )
304 _deviceOutput_node[i]->setBoolValue(true);
306 _deviceOutput_node[i]->setBoolValue(false);
308 // use default device at start
309 if( devs[i].capabilities & IAXC_AD_INPUT_DEFAULT )
310 _selectedInput_node->setIntValue(devs[i].devID);
311 if( devs[i].capabilities & IAXC_AD_OUTPUT_DEFAULT )
312 _selectedOutput_node->setIntValue(devs[i].devID);
315 // Mute the mic and set speaker at start
316 iaxc_input_level_set( 0.0 );
317 iaxc_output_level_set( _speakerLevel_node->getFloatValue() );
321 // Do the first call at start
322 const double freq = _comm0_node->getDoubleValue();
323 _currentFreqKhz = 10 * static_cast<int>(freq * 100 + 0.25);
324 std::string num = computePhoneNumber(freq, getAirportCode(freq));
326 _callComm0 = iaxc_call(num.c_str());
328 if( _callComm0 == -1 )
329 SG_LOG( SG_IO, SG_DEBUG, "FGCom: cannot call " << num.c_str() );
334 void FGCom::updateCall(bool& changed, int& callNo, double freqMHz)
337 _currentFreqKhz = 10 * static_cast<int>(freqMHz * 100 + 0.25);
340 if( !isInRange(freqMHz) ) {
341 iaxc_dump_call_number(callNo);
350 changed = false; // FIXME, out-params are confusing
353 iaxc_dump_call_number( callNo );
357 if(_p.elapsedMSec() > IAX_DELAY) {
358 std::string num = computePhoneNumber(freqMHz, getAirportCode(freqMHz));
359 if( !isInRange(freqMHz) )
362 callNo = iaxc_call(num.c_str());
365 SG_LOG( SG_IO, SG_DEBUG, "FGCom: cannot call " << num.c_str() );
374 void FGCom::update(double dt)
376 if ( !_enabled || !_initialized ) {
380 // For now we manage FGCom for only one freq because IAXClient
381 // is not able to handle multiple calls at same time.
382 updateCall(_comm0Changed, _callComm0, _comm0_node->getDoubleValue());
383 // updateCall(_comm1Changed, _callComm1, _comm1_node->getDoubleValue());
384 // updateCall(_nav0Changed, _callNav0, _nav0_node->getDoubleValue());
385 // updateCall(_nav1Changed, _callNav1, _nav1_node->getDoubleValue());
390 void FGCom::shutdown()
396 _initialized = false;
399 iaxc_set_event_callback(NULL);
400 iaxc_unregister(_regId);
401 iaxc_stop_processing_thread();
404 assert( static_instance == this );
405 static_instance = NULL;
410 void FGCom::valueChanged(SGPropertyNode *prop)
412 if (prop == _enabled_node) {
413 bool isEnabled = prop->getBoolValue();
414 if (_enabled == isEnabled) {
427 if (prop == _ptt0_node && _enabled) {
428 if( _ptt0_node->getBoolValue() ) {
429 iaxc_output_level_set( 0.0 );
430 iaxc_input_level_set( _micLevel_node->getFloatValue() ); //0.0 = min , 1.0 = max
432 iaxc_output_level_set( _speakerLevel_node->getFloatValue() );
433 iaxc_input_level_set( 0.0 );
437 if (prop == _test_node) {
438 testMode( prop->getBoolValue() );
442 //FIXME: not implemented in IAX audio driver (audio_openal.c)
443 if (prop == _micBoost_node && _initialized) {
444 int micBoost = prop->getIntValue();
445 SG_CLAMP_RANGE<int>( micBoost, 0, 1 );
446 iaxc_mic_boost_set( micBoost ) ; // 0 = enabled , 1 = disabled
450 //FIXME: not implemented in IAX audio driver (audio_openal.c)
451 if ((prop == _selectedInput_node || prop == _selectedOutput_node) && _initialized) {
452 int selectedInput = _selectedInput_node->getIntValue();
453 int selectedOutput = _selectedOutput_node->getIntValue();
454 iaxc_audio_devices_set(selectedInput, selectedOutput, 0);
458 if (_listener_active)
463 if (prop == _speakerLevel_node && _enabled) {
464 float speakerLevel = prop->getFloatValue();
465 SG_CLAMP_RANGE<float>( speakerLevel, 0.0, 1.0 );
466 _speakerLevel_node->setFloatValue(speakerLevel);
467 //iaxc_output_level_set(speakerLevel);
470 if (prop == _micLevel_node && _enabled) {
471 float micLevel = prop->getFloatValue();
472 SG_CLAMP_RANGE<float>( micLevel, 0.0, 1.0 );
473 _micLevel_node->setFloatValue(micLevel);
474 //iaxc_input_level_set(micLevel);
477 if (prop == _comm0_node) {
478 if( _currentComm0 != prop->getDoubleValue() ) {
479 _currentComm0 = prop->getDoubleValue();
481 _comm0Changed = true;
485 if (prop == _comm1_node) {
486 if( _currentComm1 != prop->getDoubleValue() ) {
487 _currentComm1 = prop->getDoubleValue();
489 _comm1Changed = true;
493 if (prop == _nav0_node) {
494 if( _currentNav0 != prop->getDoubleValue() ) {
495 _currentNav0 = prop->getDoubleValue();
500 if (prop == _nav1_node) {
501 if( _currentNav1 != prop->getDoubleValue() ) {
502 _currentNav1 = prop->getDoubleValue();
513 void FGCom::testMode(bool testMode)
515 if(testMode && _initialized) {
517 iaxc_dump_call_number(_callComm0);
518 iaxc_input_level_set( _micLevel_node->getFloatValue() );
519 iaxc_output_level_set( _speakerLevel_node->getFloatValue() );
520 std::string num = computePhoneNumber(TEST_FREQ, NULL_ICAO);
521 if( num.size() > 0 ) {
522 iaxc_millisleep(IAX_DELAY);
523 _callComm0 = iaxc_call(num.c_str());
525 if( _callComm0 == -1 )
526 SG_LOG( SG_IO, SG_DEBUG, "FGCom: cannot call " << num.c_str() );
529 iaxc_dump_call_number(_callComm0);
530 iaxc_millisleep(IAX_DELAY);
540 \param freq The requested frequency e.g 120.825
541 \return The ICAO code as string e.g LFMV
544 std::string FGCom::getAirportCode(const double& freq)
546 SGGeod aircraftPos = globals->get_aircraft_position();
548 for(size_t i=0; i<sizeof(special_freq)/sizeof(special_freq[0]); i++) { // Check if it's a special freq
549 if(special_freq[i] == _currentFreqKhz) {
554 flightgear::CommStation* apt = flightgear::CommStation::findByFreq(_currentFreqKhz, aircraftPos);
556 return std::string();
559 if( apt->type() == FGPositioned::FREQ_TOWER ) {
560 _maxRange = MAX_TWR_RANGE;
561 _minRange = MIN_GNDTWR_RANGE;
562 } else if( apt->type() == FGPositioned::FREQ_GROUND ) {
563 _maxRange = MAX_GND_RANGE;
564 _minRange = MIN_GNDTWR_RANGE;
566 _maxRange = MAX_RANGE;
567 _minRange = MIN_RANGE;
570 _aptPos = apt->geod();
571 return apt->airport()->ident();
577 \param freq The requested frequency e.g 112.7
578 \return The ICAO code as string e.g ITS
581 std::string FGCom::getVorCode(const double& freq) const
583 SGGeod aircraftPos = globals->get_aircraft_position();
584 FGNavList::TypeFilter filter(FGPositioned::VOR);
586 FGNavRecord* vor = FGNavList::findByFreq( freq, aircraftPos, &filter);
588 return std::string();
591 return vor->get_ident();
597 \param freq The requested frequency e.g 120.825
598 \param iaco The associated ICAO code e.g LFMV
599 \return The phone number as string i.e username:password@fgcom.flightgear.org/0176707786120825
602 std::string FGCom::computePhoneNumber(const double& freq, const std::string& icao) const
605 return std::string();
607 char phoneNumber[256];
611 /*Convert ICAO to ASCII */
612 sprintf( tmp, "%4s", icao.c_str() );
614 /*Built the phone number */
616 "%02d%02d%02d%02d%02d%06d",
622 (int) (freq * 1000 + 0.5) );
625 snprintf( phoneNumber,
626 sizeof (phoneNumber),
639 \return A boolean value, 1=in range, 0=out of range
642 bool FGCom::isInRange(const double &freq) const
644 for(size_t i=0; i<sizeof(special_freq)/sizeof(special_freq[0]); i++) { // Check if it's a special freq
645 if( (special_freq[i]) == _currentFreqKhz ) {
650 SGGeod acftPos = globals->get_aircraft_position();
651 double distNm = SGGeodesy::distanceNm(_aptPos, acftPos);
652 double delta_elevation_ft = fabs(acftPos.getElevationFt() - _aptPos.getElevationFt());
653 double rangeNm = 1.23 * sqrt(delta_elevation_ft);
655 if (rangeNm > _maxRange) rangeNm = _maxRange;
656 if (rangeNm < _minRange) rangeNm = _minRange;
657 if( distNm > rangeNm ) return 0;