1 // ATCmgr.cxx - Implementation of FGATCMgr - a global Flightgear ATC manager.
3 // Written by David Luff, started February 2002.
5 // Copyright (C) 2002 David C Luff - david.luff@nottingham.ac.uk
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include <simgear/misc/sg_path.hxx>
26 #include <simgear/debug/logstream.hxx>
28 #include <Airports/simple.hxx>
31 #include "commlist.hxx"
32 #include "ATCDialog.hxx"
33 #include "ATCutils.hxx"
37 // periodic radio station search wrapper
38 static void fgATCSearch( void ) {
39 globals->get_ATC_mgr()->Search();
41 */ //This wouldn't compile - including Time/event.hxx breaks it :-(
42 // Is this still true?? -EMH-
44 AirportATC::AirportATC() :
47 //airport_atc_map.clear();
51 FGATCMgr::FGATCMgr() :
53 atc_list(new atc_list_type),
54 #ifdef ENABLE_AUDIO_SUPPORT
64 FGATCMgr::~FGATCMgr() {
68 void FGATCMgr::bind() {
71 void FGATCMgr::unbind() {
74 void FGATCMgr::init() {
75 //cout << "ATCMgr::init called..." << endl;
77 lon_node = fgGetNode("/position/longitude-deg", true);
78 lat_node = fgGetNode("/position/latitude-deg", true);
79 elev_node = fgGetNode("/position/altitude-ft", true);
80 atc_list_itr = atc_list->begin();
82 // Search for connected ATC stations once per 0.8 seconds or so
83 // globals->get_event_mgr()->add( "fgATCSearch()", fgATCSearch,
84 // FGEvent::FG_EVENT_READY, 800);
86 // For some reason the above doesn't compile - including Time/event.hxx stops compilation.
87 // Is this still true after the reorganization of the event managar??
90 // Initialise the ATC Dialogs
91 SG_LOG(SG_ATC, SG_INFO, " ATC Dialog System");
92 current_atcdialog = new FGATCDialog;
93 current_atcdialog->Init();
96 //cout << "ATCmgr::init done!" << endl;
99 void FGATCMgr::update(double dt) {
102 SG_LOG(SG_ATC, SG_WARN, "Warning - ATCMgr::update(...) called before ATCMgr::init()");
105 current_atcdialog->Update(dt);
107 //cout << "Entering update..." << endl;
108 //Traverse the list of active stations.
109 //Only update one class per update step to avoid the whole ATC system having to calculate between frames.
110 //Eventually we should only update every so many steps.
111 //cout << "In FGATCMgr::update - atc_list.size = " << atc_list->size() << endl;
112 if(atc_list->size()) {
113 if(atc_list_itr == atc_list->end()) {
114 atc_list_itr = atc_list->begin();
116 //cout << "Updating " << (*atc_list_itr)->get_ident() << ' ' << (*atc_list_itr)->GetType() << '\n';
117 //cout << "Freq = " << (*atc_list_itr)->get_freq() << '\n';
118 //cout << "Updating...\n";
119 (*atc_list_itr).second->Update(dt * atc_list->size());
120 //cout << "Done ATC update..." << endl;
125 //cout << "ATC_LIST: " << atc_list->size() << ' ';
126 for(atc_list_iterator it = atc_list->begin(); it != atc_list->end(); it++) {
127 cout << (*it)->get_ident() << ' ';
132 // Search the tuned frequencies every now and then - this should be done with the event scheduler
133 static int i = 0; // Very ugly - but there should only ever be one instance of FGATCMgr.
135 //cout << "About to search navcomm1" << endl;
136 FreqSearch("comm", 0);
137 FreqSearch("nav", 0);
140 //cout << "About to search navcomm2" << endl;
141 FreqSearch("comm", 1);
142 FreqSearch("nav", 1);
147 //cout << "comm1 type = " << comm_type[0] << '\n';
148 //cout << "Leaving update..." << endl;
152 // Returns frequency in KHz - should I alter this to return in MHz?
153 unsigned short int FGATCMgr::GetFrequency(const string& ident, const atc_type& tp) {
155 bool ok = current_commlist->FindByCode(ident, test, tp);
156 return(ok ? test.freq : 0);
159 // Register the fact that the comm radio is tuned to an airport
160 // Channel is zero based
161 bool FGATCMgr::CommRegisterAirport(const string& ident, int chan, const atc_type& tp) {
162 SG_LOG(SG_ATC, SG_BULK, "Comm channel " << chan << " registered airport " << ident);
163 //cout << "Comm channel " << chan << " registered airport " << ident << ' ' << tp << '\n';
164 if(airport_atc_map.find(ident) != airport_atc_map.end()) {
165 if(tp == ATIS || tp == AWOS) {
166 airport_atc_map[ident]->atis_active = true;
170 //cout << "NOT IN MAP - creating new..." << endl;
171 const FGAirport *ap = fgFindAirportID(ident);
173 AirportATC *a = new AirportATC;
174 // I'm not entirely sure that this AirportATC structure business is actually needed - it just duplicates what we can find out anyway!
175 a->geod = ap->geod();
176 a->atis_freq = GetFrequency(ident, ATIS)
177 || GetFrequency(ident, AWOS);
178 a->atis_active = false;
179 if(tp == ATIS || tp == AWOS) {
180 a->atis_active = true;
182 airport_atc_map[ident] = a;
189 typedef map<string,int> MSI;
191 void FGATCMgr::ZapOtherService(const string ncunit, const string svc_name){
192 for (atc_list_iterator svc = atc_list->begin(); svc != atc_list->end(); svc++) {
194 if (svc->first != svc_name) {
195 MSI &actv = svc->second->active_on;
196 // OK, we have found some OTHER service;
197 // see if it is (was) active on our unit:
198 if (!actv.count(ncunit)) continue;
199 //cout << "Eradicating '" << svc->first << "' from: " << ncunit << endl;
202 //cout << "Eradicating service: '" << svc->first << "'" << endl;
203 svc->second->SetNoDisplay();
204 svc->second->Update(0); // one last update
205 SG_LOG(SG_GENERAL, SG_INFO, "would have erased ATC service:" << svc->second->get_name()<< "/"
206 << svc->second->get_ident());
207 // delete svc->second;
208 atc_list->erase(svc);
209 // ALL pointers into the ATC list are now invalid,
210 // so let's reset them:
211 atc_list_itr = atc_list->begin();
213 break; // cannot be duplicates in the active list
218 // Find in list - return a currently active ATC pointer given ICAO code and type
219 // Return NULL if the given service is not in the list
220 // - *** THE CALLING FUNCTION MUST CHECK FOR THIS ***
221 FGATC* FGATCMgr::FindInList(const string& id, const atc_type& tp) {
222 string ndx = id + decimalNumeral(tp);
223 if (!atc_list->count(ndx)) return 0;
224 return (*atc_list)[ndx];
227 // Returns true if the airport is found in the map
228 bool FGATCMgr::GetAirportATCDetails(const string& icao, AirportATC* a) {
229 if(airport_atc_map.find(icao) != airport_atc_map.end()) {
230 *a = *airport_atc_map[icao];
237 // Return a pointer to an appropriate voice for a given type of ATC
238 // creating the voice if necessary - ie. make sure exactly one copy
239 // of every voice in use exists in memory.
241 // TODO - in the future this will get more complex and dole out country/airport
242 // specific voices, and possible make sure that the same voice doesn't get used
243 // at different airports in quick succession if a large enough selection are available.
244 FGATCVoice* FGATCMgr::GetVoicePointer(const atc_type& type) {
245 // TODO - implement me better - maintain a list of loaded voices and other voices!!
248 case ATIS: case AWOS:
249 #ifdef ENABLE_AUDIO_SUPPORT
250 // Delayed loading fo all available voices, needed because the
251 // soundmanager might not be initialized (at all) at this point.
252 // For now we'll do one hardwired one
254 /* I've loaded the voice even if /sim/sound/pause is true
255 * since I know no way of forcing load of the voice if the user
256 * subsequently switches /sim/sound/audible to true.
257 * (which is the right thing to do -- CLO) :-)
259 if (!voiceOK && fgGetBool("/sim/sound/working")) {
262 voiceOK = v1->LoadVoice("default");
264 } catch ( sg_io_exception & e) {
266 SG_LOG(SG_ATC, SG_ALERT, "Unable to load default voice : "
267 << e.getFormattedMessage().c_str());
292 // Search for ATC stations by frequency
293 void FGATCMgr::FreqSearch(const string navcomm, const int unit) {
296 string ncunit = navcomm + "[" + decimalNumeral(unit) + "]";
297 string commbase = "/instrumentation/" + ncunit;
298 string commfreq = commbase + "/frequencies/selected-mhz";
299 SGPropertyNode_ptr comm_node = fgGetNode(commfreq.c_str(), false);
301 //cout << "FreqSearch: " << ncunit
302 // << " node: " << comm_node << endl;
303 if (!comm_node) return; // no such radio unit
306 double freq = comm_node->getDoubleValue();
307 _aircraftPos = SGGeod::fromDegFt(lon_node->getDoubleValue(),
308 lat_node->getDoubleValue(), elev_node->getDoubleValue());
310 // Query the data store and get the closest match if any
311 //cout << "Will FindByFreq: " << lat << " " << lon << " " << elev
312 // << " freq: " << freq << endl;
313 if(current_commlist->FindByFreq(_aircraftPos, freq, &data)) {
314 //cout << "FoundByFreq: " << freq
315 // << " ident: " << data.ident
316 // << " type: " << data.type << " ***" << endl;
317 // We are in range of something.
320 // Get rid of any *other* service that was on this radio unit:
321 string svc_name = data.ident+decimalNumeral(data.type);
322 ZapOtherService(ncunit, svc_name);
323 // See if the service already exists, possibly connected to
324 // some other radio unit:
325 if (atc_list->count(svc_name)) {
326 // make sure the service knows it's tuned on this radio:
327 FGATC* svc = (*atc_list)[svc_name];
328 svc->active_on[ncunit] = 1;
333 CommRegisterAirport(data.ident, unit, data.type);
335 // This was a switch-case statement but the compiler didn't like
336 // the new variable creation with it.
337 if(data.type == ATIS || data.type == AWOS) {
338 (*atc_list)[svc_name] = new FGATIS;
339 FGATC* svc = (*atc_list)[svc_name];
342 svc->active_on[ncunit] = 1;
348 // No services in range. Zap any service on this unit.
349 ZapOtherService(ncunit, "x x x");