]> git.mxchange.org Git - flightgear.git/blob - src/ATCDCL/ATCmgr.cxx
Merge branch 'next' of gitorious.org:fg/flightgear into next
[flightgear.git] / src / ATCDCL / ATCmgr.cxx
1 // ATCmgr.cxx - Implementation of FGATCMgr - a global Flightgear ATC manager.
2 //
3 // Written by David Luff, started February 2002.
4 //
5 // Copyright (C) 2002  David C Luff - david.luff@nottingham.ac.uk
6 //
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.
11 //
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.
16 //
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.
20
21 #ifdef HAVE_CONFIG_H
22 #  include <config.h>
23 #endif
24
25 #include <simgear/misc/sg_path.hxx>
26 #include <simgear/debug/logstream.hxx>
27
28 #include <Airports/simple.hxx>
29
30 #include "ATCmgr.hxx"
31 #include "commlist.hxx"
32 #include "ATCDialog.hxx"
33 #include "ATCutils.hxx"
34
35
36 /*
37 // periodic radio station search wrapper
38 static void fgATCSearch( void ) {
39     globals->get_ATC_mgr()->Search();
40 }
41 */ //This wouldn't compile - including Time/event.hxx breaks it :-(
42    // Is this still true?? -EMH-
43
44 AirportATC::AirportATC() :
45     atis_freq(0.0),
46     atis_active(false)
47     //airport_atc_map.clear();
48 {
49 }
50
51 FGATCMgr::FGATCMgr() :
52     initDone(false),
53     atc_list(new atc_list_type),
54 #ifdef ENABLE_AUDIO_SUPPORT
55     voice(true),
56     voiceOK(false),
57     v1(0)
58 #else
59     voice(false),
60 #endif
61 {
62 }
63
64 FGATCMgr::~FGATCMgr() {
65     delete v1;
66 }
67
68 void FGATCMgr::bind() {
69 }
70
71 void FGATCMgr::unbind() {
72 }
73
74 void FGATCMgr::init() {
75     //cout << "ATCMgr::init called..." << endl;
76     
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();
81     
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);
85         //  
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??
88         // -EMH-
89     
90     // Initialise the ATC Dialogs
91     SG_LOG(SG_ATC, SG_INFO, "  ATC Dialog System");
92     current_atcdialog = new FGATCDialog;
93     current_atcdialog->Init();
94
95     initDone = true;
96     //cout << "ATCmgr::init done!" << endl;
97 }
98
99 void FGATCMgr::update(double dt) {
100     if(!initDone) {
101         init();
102         SG_LOG(SG_ATC, SG_WARN, "Warning - ATCMgr::update(...) called before ATCMgr::init()");
103     }
104     
105     current_atcdialog->Update(dt);
106     
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();
115         }
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;
121         ++atc_list_itr;
122     }
123     
124 #ifdef ATC_TEST
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() << ' ';
128     }
129     //cout << '\n';
130 #endif
131     
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.
134     if(i == 15) {
135         //cout << "About to search navcomm1" << endl;
136         FreqSearch("comm", 0);
137         FreqSearch("nav", 0);
138     }
139     if(i == 30) {
140         //cout << "About to search navcomm2" << endl;
141         FreqSearch("comm", 1);
142         FreqSearch("nav", 1);
143         i = 0;
144     }
145     ++i;
146     
147     //cout << "comm1 type = " << comm_type[0] << '\n';
148     //cout << "Leaving update..." << endl;
149 }
150
151
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) {
154     ATCData test;
155     bool ok = current_commlist->FindByCode(ident, test, tp);
156     return(ok ? test.freq : 0);
157 }   
158
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;
167         }
168         return(true);
169     } else {
170         //cout << "NOT IN MAP - creating new..." << endl;
171         const FGAirport *ap = fgFindAirportID(ident);
172         if (ap) {
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;
181             }
182             airport_atc_map[ident] = a;
183             return(true);
184         }
185     }
186     return(false);
187 }
188
189 typedef map<string,int> MSI;
190
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++) {
193    
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;
200       actv.erase(ncunit);
201       if (!actv.size()) {
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();
212       }
213       break;        // cannot be duplicates in the active list
214     }
215   }
216 }
217
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];
225 }
226
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];
231         return(true);
232     } else {
233         return(false);
234     }
235 }
236
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.
240 //
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!!
246     if(voice) {
247         switch(type) {
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
253
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) :-)
258              */
259             if (!voiceOK && fgGetBool("/sim/sound/working")) {
260                 v1 = new FGATCVoice;
261                 try {
262                     voiceOK = v1->LoadVoice("default");
263                     voice = voiceOK;
264                 } catch ( sg_io_exception & e) {
265                     voiceOK  = false;
266                     SG_LOG(SG_ATC, SG_ALERT, "Unable to load default voice : "
267                                             << e.getFormattedMessage().c_str());
268                     voice = false;
269                     delete v1;
270                     v1 = 0;
271                 }
272             }
273 #endif
274             if(voiceOK) {
275                 return(v1);
276             }
277         case TOWER:
278             return(NULL);
279         case APPROACH:
280             return(NULL);
281         case GROUND:
282             return(NULL);
283         default:
284             return(NULL);
285         }
286         return(NULL);
287     } else {
288         return(NULL);
289     }
290 }
291
292 // Search for ATC stations by frequency
293 void FGATCMgr::FreqSearch(const string navcomm, const int unit) {
294
295
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);
300
301     //cout << "FreqSearch: " << ncunit
302     //  << "  node: " << comm_node << endl;
303     if (!comm_node) return; // no such radio unit
304
305     ATCData data;   
306     double freq = comm_node->getDoubleValue();
307     _aircraftPos = SGGeod::fromDegFt(lon_node->getDoubleValue(),
308         lat_node->getDoubleValue(), elev_node->getDoubleValue());
309     
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.
318
319
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;
329             svc->SetDisplay();
330             return;
331         }
332
333         CommRegisterAirport(data.ident, unit, data.type);
334
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];
340             if(svc != NULL) {
341                 svc->SetData(&data);
342                 svc->active_on[ncunit] = 1;
343                 svc->SetDisplay();
344                 svc->Init();
345             }
346         }
347     } else {
348         // No services in range.  Zap any service on this unit.
349         ZapOtherService(ncunit, "x x x");
350     } 
351 }