| 1 | /* |
| 2 | * fgcom - VoIP-Client for the FlightGear-Radio-Infrastructure |
| 3 | * |
| 4 | * This program realizes the usage of the VoIP infractructure based |
| 5 | * on flight data which is send from FlightGear with an external |
| 6 | * protocol to this application. |
| 7 | * |
| 8 | * Clement de l'Hamaide - Jan 2014 |
| 9 | * Re-writting of FGCom standalone |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU General Public License as |
| 13 | * published by the Free Software Foundation; either version 2 of the |
| 14 | * License, or (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, but |
| 17 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 | * General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 24 | * MA 02110-1301, USA. |
| 25 | * |
| 26 | */ |
| 27 | |
| 28 | #include "config.h" |
| 29 | |
| 30 | #include <simgear/sg_inlines.h> |
| 31 | #include <simgear/math/SGMath.hxx> |
| 32 | #include <simgear/io/raw_socket.hxx> |
| 33 | #include <simgear/misc/strutils.hxx> |
| 34 | #include <simgear/debug/logstream.hxx> |
| 35 | #include <simgear/timing/timestamp.hxx> |
| 36 | |
| 37 | #include <iaxclient.h> |
| 38 | |
| 39 | #include "fgcom_external.hxx" |
| 40 | #include "positions.hxx" // provides _positionsData[]; |
| 41 | |
| 42 | int _port = 16661; |
| 43 | int _callId = -1; |
| 44 | int _currentFreqKhz = -1; |
| 45 | int _maxRange = 100; |
| 46 | int _minRange = 10; |
| 47 | int _registrationId = -1; |
| 48 | bool _libInitialized = false; |
| 49 | bool _running = true; |
| 50 | bool _debug = false; |
| 51 | bool _connected = false; |
| 52 | double _frequency = -1; |
| 53 | double _atis = -1; |
| 54 | double _silenceThd = -35.0; |
| 55 | std::string _app = "FGCOM-"; |
| 56 | std::string _host = "127.0.0.1"; |
| 57 | std::string _server = "fgcom.flightgear.org"; |
| 58 | std::string _airport = "ZZZZ"; |
| 59 | std::string _callsign = "guest"; |
| 60 | std::string _username = "guest"; |
| 61 | std::string _password = "guest"; |
| 62 | |
| 63 | SGGeod _airportPos; |
| 64 | SGTimeStamp _p; |
| 65 | std::multimap<int, Airport> _airportsData; |
| 66 | |
| 67 | const int special_freq[] = { // Define some freq who need to be used with icao = ZZZZ |
| 68 | 910000, |
| 69 | 911000, |
| 70 | 700000, |
| 71 | 123450, |
| 72 | 122750, |
| 73 | 121500, |
| 74 | 123500 }; |
| 75 | |
| 76 | // |
| 77 | // Main loop |
| 78 | // |
| 79 | |
| 80 | int main(int argc, char** argv) |
| 81 | { |
| 82 | signal(SIGINT, quit); |
| 83 | signal(SIGTERM, quit); |
| 84 | |
| 85 | simgear::requestConsole(true); |
| 86 | sglog().setLogLevels(SG_ALL, SG_INFO); |
| 87 | _app += FGCOM_VERSION; |
| 88 | Modes mode = PILOT; |
| 89 | std::string num = ""; |
| 90 | |
| 91 | for(int count = 1; count < argc; count++) { |
| 92 | std::string item = argv[count]; |
| 93 | std::string option = item.substr(2, item.find("=")-2); |
| 94 | std::string value = item.substr(item.find("=")+1, item.size()); |
| 95 | if(option == "server") _server = value; |
| 96 | if(option == "host") _host = value; |
| 97 | if(option == "port") _port = atoi(value.c_str()); |
| 98 | if(option == "callsign") _callsign = value; |
| 99 | if(option == "frequency") _frequency = atof(value.c_str()); |
| 100 | if(option == "atis") _atis = atof(value.c_str()); |
| 101 | if(option == "airport") _airport = simgear::strutils::uppercase(value); |
| 102 | if(option == "username") _username = value; |
| 103 | if(option == "password") _password = value; |
| 104 | if(option == "silence-threshold") _silenceThd = atof(value.c_str()); |
| 105 | if(option == "debug") sglog().setLogLevels(SG_ALL, SG_DEBUG); |
| 106 | if(option == "help") return usage(); |
| 107 | if(option == "version") return version(); |
| 108 | } |
| 109 | |
| 110 | if(_frequency == 910.000) |
| 111 | mode = TEST; |
| 112 | if(_frequency <= 136.975 && _frequency >= 118.000) |
| 113 | mode = OBS; |
| 114 | if(_atis <= 136.975 && _atis >= 118.000 && _airport != "ZZZZ") |
| 115 | mode = ATC; |
| 116 | |
| 117 | SG_LOG(SG_GENERAL, SG_INFO, "FGCom " << FGCOM_VERSION << " compiled " << __DATE__ |
| 118 | << ", at " << __TIME__ ); |
| 119 | SG_LOG(SG_GENERAL, SG_INFO, "For help usage, use --help"); |
| 120 | SG_LOG(SG_GENERAL, SG_INFO, "Starting FGCom session as " << _username << ":xxxxxxxxx@" << _server); |
| 121 | |
| 122 | if( !(_libInitialized = lib_init()) ) |
| 123 | return EXIT_FAILURE; |
| 124 | |
| 125 | if (_username != "guest" && _password != "guest") |
| 126 | _registrationId = lib_registration(); |
| 127 | |
| 128 | if(mode == PILOT) { |
| 129 | SG_LOG( SG_GENERAL, SG_DEBUG, "Entering main loop in mode PILOT" ); |
| 130 | |
| 131 | simgear::Socket::initSockets(); |
| 132 | simgear::Socket sgSocket; |
| 133 | sgSocket.open(false); |
| 134 | sgSocket.bind(_host.c_str(), _port); |
| 135 | sgSocket.setBlocking(false); |
| 136 | lib_setVolume(0.0, 1.0); |
| 137 | static char currentPacket[MAXBUFLEN+2], previousPacket[MAXBUFLEN+2]; |
| 138 | struct Data currentData, previousData, previousPosData; |
| 139 | double currentFreq = -1, previousFreq = -1; |
| 140 | std::string currentIcao = ""; |
| 141 | ActiveComm activeComm = COM1; |
| 142 | |
| 143 | _airportsData = getAirportsData(); |
| 144 | SG_LOG(SG_GENERAL, SG_INFO, ""); |
| 145 | |
| 146 | while(_running) { |
| 147 | int bytes = sgSocket.recv(currentPacket, sizeof(currentPacket)-1, 0); |
| 148 | if (bytes == -1) { |
| 149 | SGTimeStamp::sleepForMSec(1); // Prevent full CPU usage (loop) |
| 150 | continue; |
| 151 | } |
| 152 | |
| 153 | currentPacket[bytes] = '\0'; |
| 154 | if( strcmp(currentPacket, previousPacket) != 0 ) { |
| 155 | std::string packet(currentPacket); |
| 156 | std::vector<std::string> properties = simgear::strutils::split(packet, ","); |
| 157 | for(size_t i=0; i < properties.size(); i++) { |
| 158 | std::vector<std::string> prop = simgear::strutils::split(properties[i], "="); |
| 159 | if(prop[0] == "PTT") currentData.ptt = atoi(prop[1].c_str()); |
| 160 | if(prop[0] == "LAT") currentData.lat = atof(prop[1].c_str()); |
| 161 | if(prop[0] == "LON") currentData.lon = atof(prop[1].c_str()); |
| 162 | if(prop[0] == "ALT") currentData.alt = atof(prop[1].c_str()); |
| 163 | if(prop[0] == "COM1_FRQ") currentData.com1 = atof(prop[1].c_str()); |
| 164 | if(prop[0] == "COM2_FRQ") currentData.com2 = atof(prop[1].c_str()); |
| 165 | if(prop[0] == "OUTPUT_VOL") currentData.outputVol = atof(prop[1].c_str()); |
| 166 | if(prop[0] == "SILENCE_THD") currentData.silenceThd = atof(prop[1].c_str()); |
| 167 | if(prop[0] == "CALLSIGN") currentData.callsign = prop[1]; |
| 168 | } |
| 169 | |
| 170 | if(currentData.ptt != previousData.ptt) { |
| 171 | if(currentData.ptt == 2) { |
| 172 | if(activeComm == COM1) { |
| 173 | activeComm = COM2; |
| 174 | currentFreq = currentData.com2; |
| 175 | } else { |
| 176 | activeComm = COM1; |
| 177 | currentFreq = currentData.com1; |
| 178 | } |
| 179 | SG_LOG( SG_GENERAL, SG_INFO, "Select radio " << activeComm << " on " << currentFreq << " MHz" ); |
| 180 | } else if(currentData.ptt) { |
| 181 | SG_LOG( SG_GENERAL, SG_INFO, "[SPEAK] unmute mic, mute speaker" ); |
| 182 | lib_setVolume(1.0, 0.0); |
| 183 | } else { |
| 184 | SG_LOG( SG_GENERAL, SG_INFO, "[LISTEN] mute mic, unmute speaker" ); |
| 185 | lib_setVolume(0.0, currentData.outputVol); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | if(currentData.outputVol != previousData.outputVol) |
| 190 | lib_setVolume(0.0, currentData.outputVol); |
| 191 | |
| 192 | if(currentData.silenceThd != previousData.silenceThd) |
| 193 | lib_setSilenceThreshold(currentData.silenceThd); |
| 194 | |
| 195 | if(currentData.callsign != previousData.callsign) |
| 196 | lib_setCallerId(currentData.callsign); |
| 197 | |
| 198 | if(currentData.com1 != previousData.com1 && activeComm == COM1) { |
| 199 | currentFreq = currentData.com1; |
| 200 | SG_LOG( SG_GENERAL, SG_INFO, "Select frequency " << currentFreq << " MHz on radio " << activeComm ); |
| 201 | } |
| 202 | |
| 203 | if(currentData.com2 != previousData.com2 && activeComm == COM2) { |
| 204 | currentFreq = currentData.com2; |
| 205 | SG_LOG( SG_GENERAL, SG_INFO, "Select frequency " << currentFreq << " MHz on radio " << activeComm ); |
| 206 | } |
| 207 | |
| 208 | if(previousFreq != currentFreq || currentData.callsign != previousData.callsign) { |
| 209 | _currentFreqKhz = 10 * static_cast<int>(currentFreq * 100 + 0.25); |
| 210 | currentIcao = getClosestAirportForFreq(currentFreq, currentData.lat, currentData.lon, currentData.alt); |
| 211 | |
| 212 | if(isInRange(currentIcao, currentData.lat, currentData.lon, currentData.alt)) { |
| 213 | _connected = lib_call(currentIcao, currentFreq); |
| 214 | SG_LOG( SG_GENERAL, SG_INFO, "Connecting " << currentIcao << " on " << currentFreq << " MHz" ); |
| 215 | } else { |
| 216 | if(_connected) { |
| 217 | _connected = lib_hangup(); |
| 218 | SG_LOG( SG_GENERAL, SG_INFO, "Disconnecting " << currentIcao << " on " << currentFreq << " MHz (out of range)" ); |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | if( currentData.lat <= previousPosData.lat - 0.05 || |
| 224 | currentData.lon <= previousPosData.lon - 0.05 || |
| 225 | currentData.alt <= previousPosData.alt - 50.0 || |
| 226 | currentData.lat >= previousPosData.lat + 0.05 || |
| 227 | currentData.lon >= previousPosData.lon + 0.05 || |
| 228 | currentData.alt >= previousPosData.alt + 50.0) { |
| 229 | |
| 230 | currentIcao = getClosestAirportForFreq(currentFreq, currentData.lat, currentData.lon, currentData.alt); |
| 231 | if(_connected) { |
| 232 | if(!isInRange(currentIcao, currentData.lat, currentData.lon, currentData.alt)) { |
| 233 | _connected = lib_hangup(); |
| 234 | SG_LOG( SG_GENERAL, SG_INFO, "Disconnecting " << currentIcao << " on " << currentFreq << " MHz (out of range)" ); |
| 235 | } |
| 236 | } else { |
| 237 | if(isInRange(currentIcao, currentData.lat, currentData.lon, currentData.alt)) { |
| 238 | _connected = lib_call(currentIcao, currentFreq); |
| 239 | SG_LOG( SG_GENERAL, SG_INFO, "Connecting " << currentIcao << " on " << currentFreq << " MHz" ); |
| 240 | } |
| 241 | } |
| 242 | previousPosData = currentData; |
| 243 | } |
| 244 | previousFreq = currentFreq; |
| 245 | previousData = currentData; |
| 246 | } |
| 247 | strcpy(previousPacket, currentPacket); |
| 248 | } // while() |
| 249 | sgSocket.close(); |
| 250 | } else { // if(mode == PILOT) |
| 251 | int sessionDuration = 1000; |
| 252 | _p.stamp(); |
| 253 | if(mode == OBS) { |
| 254 | SG_LOG( SG_GENERAL, SG_DEBUG, "Entering main loop in mode OBS (max duration: 6 hours)" ); |
| 255 | sessionDuration *= 2160; // 6 hours for OBS mode |
| 256 | lib_setVolume(0.0, 1.0); |
| 257 | lib_setCallerId("::OBS::"); |
| 258 | num = computePhoneNumber(_frequency, _airport); |
| 259 | } else { |
| 260 | lib_setVolume(1.0, 1.0); |
| 261 | if(mode == TEST) { |
| 262 | sessionDuration *= 65; // 65 seconds for TEST mode |
| 263 | SG_LOG( SG_GENERAL, SG_DEBUG, "Entering main loop in mode TEST (max duration: 65 seconds)" ); |
| 264 | _airport = "ZZZZ"; |
| 265 | num = computePhoneNumber(_frequency, _airport); |
| 266 | } else if(mode == ATC) { |
| 267 | sessionDuration *= 45; // 45 seconds for ATC mode |
| 268 | SG_LOG( SG_GENERAL, SG_DEBUG, "Entering main loop in mode ATC (max duration: 45 seconds)" ); |
| 269 | num = computePhoneNumber(_atis, _airport, true); |
| 270 | } |
| 271 | } |
| 272 | _connected = lib_directCall(_airport, _frequency, num); |
| 273 | |
| 274 | while (_p.elapsedMSec() <= sessionDuration && _running){ |
| 275 | SGTimeStamp::sleepForMSec(2000); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | if(!lib_shutdown()) |
| 280 | return EXIT_FAILURE; |
| 281 | return EXIT_SUCCESS; |
| 282 | } |
| 283 | |
| 284 | // function: getAirportsData |
| 285 | // action: parse positionsData.hxx then build multimap |
| 286 | |
| 287 | std::multimap<int, Airport> getAirportsData() |
| 288 | { |
| 289 | std::vector<std::string> lines; |
| 290 | std::multimap<int, Airport> aptData; |
| 291 | SG_LOG(SG_GENERAL, SG_INFO, "Loading airports information..."); |
| 292 | |
| 293 | for(size_t i=0; i < sizeof(_positionsData)/sizeof(*_positionsData); i++) { // _positionsData is provided by positions.hxx |
| 294 | std::vector<std::string> entries = simgear::strutils::split(_positionsData[i], ","); |
| 295 | if(entries.size() == 6) { |
| 296 | // [0]=ICAO, [1]=Frequency, [2]=Latitude, [3]=Longitude, [4]=ID/Type, [5]=Name |
| 297 | std::string entryIcao = entries[0]; |
| 298 | double entryFreq = atof(entries[1].c_str()); |
| 299 | double entryLat = atof(entries[2].c_str()); |
| 300 | double entryLon = atof(entries[3].c_str()); |
| 301 | std::string entryType = entries[4]; |
| 302 | std::string entryName = entries[5]; |
| 303 | |
| 304 | int aptFreqKhz = 10 * static_cast<int>(entryFreq * 100 + 0.25); |
| 305 | Airport apt; |
| 306 | apt.icao = entryIcao; |
| 307 | apt.frequency = entryFreq; |
| 308 | apt.latitude = entryLat; |
| 309 | apt.longitude = entryLon; |
| 310 | apt.type = entryType; |
| 311 | apt.name = entryName; |
| 312 | aptData.insert( std::pair<int, Airport>(aptFreqKhz, apt) ); |
| 313 | } |
| 314 | } |
| 315 | return aptData; |
| 316 | } |
| 317 | |
| 318 | // function: orderByDistanceNm |
| 319 | // action: sort airportsInRange vector by distanceNm ASC in getClosestAirportForFreq() |
| 320 | |
| 321 | bool orderByDistanceNm(Airport a, Airport b) |
| 322 | { |
| 323 | return a.distanceNm < b.distanceNm; |
| 324 | } |
| 325 | |
| 326 | // function: gestClosestAircraftForFreq |
| 327 | // action: return ICAO of closest airport with given frequency and define his position |
| 328 | |
| 329 | std::string getClosestAirportForFreq(double freq, double acftLat, double acftLon, double acftAlt) |
| 330 | { |
| 331 | for(size_t i=0; i<sizeof(special_freq)/sizeof(special_freq[0]); i++) { // Check if it's a special freq |
| 332 | if( special_freq[i] == _currentFreqKhz ) |
| 333 | return std::string("ZZZZ"); |
| 334 | } |
| 335 | |
| 336 | std::string icao = ""; |
| 337 | double aptLon = 0; |
| 338 | double aptLat = 0; |
| 339 | int freqKhz = 10 * static_cast<int>(freq * 100 + 0.25); |
| 340 | SGGeod acftPos = SGGeod::fromDegFt(acftLon, acftLat, acftAlt); |
| 341 | std::vector<Airport> airportsInRange; |
| 342 | |
| 343 | std::pair <std::multimap<int, Airport>::iterator, std::multimap<int, Airport>::iterator> ret; |
| 344 | ret = _airportsData.equal_range(freqKhz); |
| 345 | for (std::multimap<int, Airport>::iterator it=ret.first; it!=ret.second; ++it) { |
| 346 | SGGeod aptPos = SGGeod::fromDeg(it->second.longitude, it->second.latitude); |
| 347 | double distNm = SGGeodesy::distanceNm(aptPos, acftPos); |
| 348 | if(distNm <= _maxRange){ |
| 349 | it->second.distanceNm = distNm; |
| 350 | airportsInRange.push_back(it->second); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | if(!airportsInRange.size()) |
| 355 | return icao; |
| 356 | |
| 357 | std::sort(airportsInRange.begin(), airportsInRange.end(), orderByDistanceNm); |
| 358 | |
| 359 | aptLon = airportsInRange[0].longitude; |
| 360 | aptLat = airportsInRange[0].latitude; |
| 361 | icao = airportsInRange[0].icao; |
| 362 | _airportPos = SGGeod::fromDeg(aptLon, aptLat); |
| 363 | |
| 364 | SG_LOG(SG_GENERAL, SG_INFO, "Airport " << airportsInRange[0].icao << " " << airportsInRange[0].name << " - " |
| 365 | << airportsInRange[0].type << " on " << airportsInRange[0].frequency |
| 366 | << " - is in range " << airportsInRange[0].distanceNm << "nm (" |
| 367 | << (SG_NM_TO_METER*airportsInRange[0].distanceNm)/1000 <<"km)"); |
| 368 | return icao; |
| 369 | } |
| 370 | |
| 371 | // function: isInRange |
| 372 | // action: return TRUE if airport/freq is in range, else return FALSE |
| 373 | |
| 374 | bool isInRange(std::string icao, double acftLat, double acftLon, double acftAlt) |
| 375 | { |
| 376 | for(size_t i=0; i<sizeof(special_freq)/sizeof(special_freq[0]); i++) { // Check if it's a special freq |
| 377 | if( special_freq[i] == _currentFreqKhz ) |
| 378 | return true; |
| 379 | } |
| 380 | |
| 381 | if(icao.empty()) |
| 382 | return false; |
| 383 | |
| 384 | SGGeod acftPos = SGGeod::fromDegFt(acftLon, acftLat, acftAlt); |
| 385 | double distNm = SGGeodesy::distanceNm(_airportPos, acftPos); |
| 386 | double delta_elevation_ft = fabs(acftPos.getElevationFt() - _airportPos.getElevationFt()); |
| 387 | double rangeNm = 1.23 * sqrt(delta_elevation_ft); |
| 388 | |
| 389 | if (rangeNm > _maxRange) rangeNm = _maxRange; |
| 390 | if (rangeNm < _minRange) rangeNm = _minRange; |
| 391 | if( distNm > rangeNm ) return false; |
| 392 | return true; |
| 393 | } |
| 394 | |
| 395 | // function: quit |
| 396 | // action: set _running flag to false |
| 397 | |
| 398 | void quit(int state) |
| 399 | { |
| 400 | SG_LOG( SG_GENERAL, SG_INFO, "Exiting FGCom" ); |
| 401 | _running = false; |
| 402 | #ifdef _WIN32 |
| 403 | lib_shutdown(); |
| 404 | SG_LOG(SG_GENERAL, SG_INFO, "You can close the terminal now"); |
| 405 | #endif |
| 406 | } |
| 407 | |
| 408 | // function: usage |
| 409 | // action: display FGCom usage then quit |
| 410 | |
| 411 | int usage() |
| 412 | { |
| 413 | std::cout << "FGCom " << FGCOM_VERSION << " usage:" << std::endl; |
| 414 | std::cout << " --server=fgcom.flightgear.org - Server to connect" << std::endl; |
| 415 | std::cout << " --host=127.0.0.1 - Host to listen i.e where FG is running" << std::endl; |
| 416 | std::cout << " --port=16661 - Port to use" << std::endl; |
| 417 | std::cout << " --callsign=guest - Callsign during session e.g F-ELYD" << std::endl; |
| 418 | std::cout << " --frequency=xxx.xxx - Frequency e.g 120.500" << std::endl; |
| 419 | std::cout << " --airport=YYYY - ICAO of airport e.g KSFO" << std::endl; |
| 420 | std::cout << " --username=guest - Username for registration" << std::endl; |
| 421 | std::cout << " --password=guest - Password for registration" << std::endl; |
| 422 | std::cout << " --silence-threshold=-35 - Silence threshold in dB (-60 < range < 0 )" << std::endl; |
| 423 | std::cout << " --debug - Enable debug output" << std::endl; |
| 424 | std::cout << " --help - Show this message" << std::endl; |
| 425 | std::cout << " --version - Show version" << std::endl; |
| 426 | std::cout << "" << std::endl; |
| 427 | std::cout << " None of these options are required, you can simply start FGCom without option at all: it works" << std::endl; |
| 428 | std::cout << " For further information, please visit: http://wiki.flightgear.org/FGCom_3.0" << std::endl; |
| 429 | std::cout << "" << std::endl; |
| 430 | std::cout << " About silence-threshold:" << std::endl; |
| 431 | std::cout << " This is the limit, in dB, when FGCom consider no voice in your microphone." << std::endl; |
| 432 | std::cout << " --silence-threshold=-60 is similar to micro always ON" << std::endl; |
| 433 | std::cout << " --silence-threshold=0 is similar to micro always OFF" << std::endl; |
| 434 | std::cout << " Default value is -35.0 dB" << std::endl; |
| 435 | std::cout << "" << std::endl; |
| 436 | std::cout << " In order to make an echo-test, you have to start FGCom like:" << std::endl; |
| 437 | std::cout << " fgcom --frequency=910" << std::endl; |
| 438 | std::cout << "" << std::endl; |
| 439 | std::cout << " In order to listen a frequency, you have to start FGCom like:" << std::endl; |
| 440 | std::cout << " fgcom --frequency=xxx.xxx --airport=YYYY" << std::endl; |
| 441 | std::cout << " where xxx.xxx is the frequency of the ICAO airport YYYY that you want to listen to" << std::endl; |
| 442 | std::cout << "" << std::endl; |
| 443 | std::cout << " In order to record an ATIS message, you have to start FGCom like:" << std::endl; |
| 444 | std::cout << " fgcom --atis=xxx.xxx --airport=YYYY" << std::endl; |
| 445 | std::cout << " where xxx.xxx is the ATIS frequency of the ICAO airport YYYY" << std::endl; |
| 446 | std::cout << "" << std::endl; |
| 447 | return EXIT_SUCCESS; |
| 448 | } |
| 449 | |
| 450 | // function: version |
| 451 | // action: display FGCom version then quit |
| 452 | |
| 453 | int version() |
| 454 | { |
| 455 | SG_LOG(SG_GENERAL, SG_INFO, "FGCom " << FGCOM_VERSION << " compiled " << __DATE__ |
| 456 | << ", at " << __TIME__ ); |
| 457 | std::cout << "" << std::endl; |
| 458 | return EXIT_SUCCESS; |
| 459 | } |
| 460 | |
| 461 | // function: computePhoneNumber |
| 462 | // action: return phone number |
| 463 | |
| 464 | std::string computePhoneNumber(double freq, std::string icao, bool atis) |
| 465 | { |
| 466 | if(icao.empty()) |
| 467 | return std::string(); |
| 468 | |
| 469 | char phoneNumber[256]; |
| 470 | char exten[32]; |
| 471 | char tmp[5]; |
| 472 | int prefix = atis ? 99 : 01; |
| 473 | |
| 474 | snprintf(tmp, sizeof(tmp), "%4s", icao.c_str()); |
| 475 | |
| 476 | snprintf(exten, sizeof(exten), |
| 477 | "%02d%02d%02d%02d%02d%06d", |
| 478 | prefix, |
| 479 | tmp[0], |
| 480 | tmp[1], |
| 481 | tmp[2], |
| 482 | tmp[3], |
| 483 | (int)(freq * 1000 + 0.5)); |
| 484 | exten[16] = '\0'; |
| 485 | |
| 486 | snprintf( phoneNumber, |
| 487 | sizeof(phoneNumber), |
| 488 | "%s:%s@%s/%s", |
| 489 | _username.c_str(), |
| 490 | _password.c_str(), |
| 491 | _server.c_str(), |
| 492 | exten); |
| 493 | return phoneNumber; |
| 494 | } |
| 495 | |
| 496 | // function: lib_setVolume |
| 497 | // action: set input/output volume |
| 498 | |
| 499 | void lib_setVolume(double input, double output) |
| 500 | { |
| 501 | SG_CLAMP_RANGE<double>(input, 0.0, 1.0); |
| 502 | SG_CLAMP_RANGE<double>(output, 0.0, 1.0); |
| 503 | SG_LOG(SG_GENERAL, SG_DEBUG, "Set volume input=" << input << " , output=" << output); |
| 504 | iaxc_input_level_set(input); |
| 505 | iaxc_output_level_set(output); |
| 506 | } |
| 507 | |
| 508 | // function: lib_setSilenceThreshold |
| 509 | // action: set silence threshold |
| 510 | |
| 511 | void lib_setSilenceThreshold(double thd) |
| 512 | { |
| 513 | SG_CLAMP_RANGE<double>(thd, -60, 0); |
| 514 | SG_LOG(SG_GENERAL, SG_DEBUG, "Set silence threshold=" << thd); |
| 515 | iaxc_set_silence_threshold(thd); |
| 516 | } |
| 517 | |
| 518 | // function: lib_setCallerId |
| 519 | // action: set caller id for the session |
| 520 | |
| 521 | void lib_setCallerId(std::string callsign) |
| 522 | { |
| 523 | SG_LOG(SG_GENERAL, SG_DEBUG, "Set caller ID=" << callsign); |
| 524 | iaxc_set_callerid (callsign.c_str(), _app.c_str()); |
| 525 | } |
| 526 | |
| 527 | // function: lib_init |
| 528 | // action: init the library |
| 529 | |
| 530 | bool lib_init() |
| 531 | { |
| 532 | SG_LOG(SG_GENERAL, SG_DEBUG, "Initializing IAX library"); |
| 533 | #ifdef _MSC_VER |
| 534 | iaxc_set_networking( (iaxc_sendto_t)sendto, (iaxc_recvfrom_t)recvfrom ); |
| 535 | #endif |
| 536 | if (iaxc_initialize(4)) { |
| 537 | SG_LOG( SG_GENERAL, SG_ALERT, "Error: cannot initialize IAXClient !\nHINT: Have you checked the mic and speakers ?" ); |
| 538 | return false; |
| 539 | } |
| 540 | |
| 541 | iaxc_set_callerid( _callsign.c_str(), _app.c_str() ); |
| 542 | iaxc_set_formats(IAXC_FORMAT_SPEEX, IAXC_FORMAT_ULAW|IAXC_FORMAT_SPEEX); |
| 543 | iaxc_set_speex_settings(1, 5, 0, 1, 0, 3); |
| 544 | iaxc_set_filters(IAXC_FILTER_AGC | IAXC_FILTER_DENOISE); |
| 545 | iaxc_set_event_callback(iaxc_callback); |
| 546 | iaxc_start_processing_thread (); |
| 547 | lib_setSilenceThreshold(_silenceThd); |
| 548 | return true; |
| 549 | } |
| 550 | |
| 551 | // function: lib_shutdown |
| 552 | // action: stop the library |
| 553 | |
| 554 | bool lib_shutdown() |
| 555 | { |
| 556 | SG_LOG(SG_GENERAL, SG_DEBUG, "Shutdown IAX library"); |
| 557 | lib_hangup(); |
| 558 | if(_registrationId != -1) |
| 559 | iaxc_unregister(_registrationId); |
| 560 | return true; |
| 561 | } |
| 562 | |
| 563 | // function: lib_call |
| 564 | // action: register a user on remote server then return the registration ID |
| 565 | |
| 566 | int lib_registration() |
| 567 | { |
| 568 | SG_LOG(SG_GENERAL, SG_DEBUG, "Request registration"); |
| 569 | SG_LOG(SG_GENERAL, SG_DEBUG, " username: " << _username); |
| 570 | SG_LOG(SG_GENERAL, SG_DEBUG, " password: xxxxxxxx"); |
| 571 | SG_LOG(SG_GENERAL, SG_DEBUG, " server: " << _server); |
| 572 | int regId = iaxc_register( _username.c_str(), _password.c_str(), _server.c_str()); |
| 573 | if(regId == -1) { |
| 574 | SG_LOG( SG_GENERAL, SG_ALERT, "Warning: cannot register '" << _username << "' at '" << _server ); |
| 575 | } |
| 576 | return regId; |
| 577 | } |
| 578 | |
| 579 | // function: lib_call |
| 580 | // action: kill current call then do a new call |
| 581 | |
| 582 | bool lib_call(std::string icao, double freq) |
| 583 | { |
| 584 | SG_LOG(SG_GENERAL, SG_DEBUG, "Request new call"); |
| 585 | SG_LOG(SG_GENERAL, SG_DEBUG, " icao: " << icao); |
| 586 | SG_LOG(SG_GENERAL, SG_DEBUG, " freq: " << freq); |
| 587 | lib_hangup(); |
| 588 | iaxc_millisleep(300); |
| 589 | std::string num = computePhoneNumber(freq, icao); |
| 590 | if(num.empty()) |
| 591 | return false; |
| 592 | _callId = iaxc_call(num.c_str()); |
| 593 | if(_callId == -1) { |
| 594 | SG_LOG( SG_GENERAL, SG_ALERT, "Warning: cannot call: " << num ); |
| 595 | return false; |
| 596 | } |
| 597 | return true; |
| 598 | } |
| 599 | |
| 600 | bool lib_directCall(std::string icao, double freq, std::string num) |
| 601 | { |
| 602 | SG_LOG(SG_GENERAL, SG_DEBUG, "Request new call"); |
| 603 | SG_LOG(SG_GENERAL, SG_DEBUG, " icao: " << icao); |
| 604 | SG_LOG(SG_GENERAL, SG_DEBUG, " freq: " << freq); |
| 605 | lib_hangup(); |
| 606 | iaxc_millisleep(300); |
| 607 | if(num.empty()) |
| 608 | return false; |
| 609 | _callId = iaxc_call(num.c_str()); |
| 610 | if(_callId == -1) { |
| 611 | SG_LOG( SG_GENERAL, SG_ALERT, "Warning: cannot call: " << num ); |
| 612 | return false; |
| 613 | } |
| 614 | return true; |
| 615 | } |
| 616 | |
| 617 | // function: lib_hangup |
| 618 | // action: kill current call |
| 619 | |
| 620 | bool lib_hangup() |
| 621 | { |
| 622 | if(!_connected) |
| 623 | return false; |
| 624 | SG_LOG(SG_GENERAL, SG_DEBUG, "Request hangup"); |
| 625 | iaxc_dump_all_calls(); |
| 626 | _callId = -1; |
| 627 | return false; |
| 628 | } |
| 629 | |
| 630 | // function: iaxc_callback |
| 631 | // action: parse IAX event then call event handler |
| 632 | |
| 633 | int iaxc_callback(iaxc_event e) |
| 634 | { |
| 635 | switch (e.type) { |
| 636 | case IAXC_EVENT_TEXT: |
| 637 | if(e.ev.text.type == IAXC_TEXT_TYPE_STATUS || |
| 638 | e.ev.text.type == IAXC_TEXT_TYPE_IAX) |
| 639 | SG_LOG( SG_GENERAL, SG_INFO, "Message: " << e.ev.text.message ); |
| 640 | break; |
| 641 | } |
| 642 | return 1; |
| 643 | } |
| 644 | // eof |