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
65 FGATCMgr::~FGATCMgr() {
69 void FGATCMgr::bind() {
72 void FGATCMgr::unbind() {
75 void FGATCMgr::init() {
76 //cout << "ATCMgr::init called..." << endl;
78 lon_node = fgGetNode("/position/longitude-deg", true);
79 lat_node = fgGetNode("/position/latitude-deg", true);
80 elev_node = fgGetNode("/position/altitude-ft", true);
81 atc_list_itr = atc_list->begin();
83 // Search for connected ATC stations once per 0.8 seconds or so
84 // globals->get_event_mgr()->add( "fgATCSearch()", fgATCSearch,
85 // FGEvent::FG_EVENT_READY, 800);
87 // For some reason the above doesn't compile - including Time/event.hxx stops compilation.
88 // Is this still true after the reorganization of the event managar??
91 // Initialise the ATC Dialogs
92 SG_LOG(SG_ATC, SG_INFO, " ATC Dialog System");
93 current_atcdialog = new FGATCDialog;
94 current_atcdialog->Init();
97 //cout << "ATCmgr::init done!" << endl;
100 void FGATCMgr::update(double dt) {
103 SG_LOG(SG_ATC, SG_WARN, "Warning - ATCMgr::update(...) called before ATCMgr::init()");
106 current_atcdialog->Update(dt);
108 //cout << "Entering update..." << endl;
109 //Traverse the list of active stations.
110 //Only update one class per update step to avoid the whole ATC system having to calculate between frames.
111 //Eventually we should only update every so many steps.
112 //cout << "In FGATCMgr::update - atc_list.size = " << atc_list->size() << endl;
113 if(atc_list->size()) {
114 if(atc_list_itr == atc_list->end()) {
115 atc_list_itr = atc_list->begin();
117 //cout << "Updating " << (*atc_list_itr)->get_ident() << ' ' << (*atc_list_itr)->GetType() << '\n';
118 //cout << "Freq = " << (*atc_list_itr)->get_freq() << '\n';
119 //cout << "Updating...\n";
120 (*atc_list_itr).second->Update(dt * atc_list->size());
121 //cout << "Done ATC update..." << endl;
126 //cout << "ATC_LIST: " << atc_list->size() << ' ';
127 for(atc_list_iterator it = atc_list->begin(); it != atc_list->end(); it++) {
128 cout << (*it)->get_ident() << ' ';
133 // Search the tuned frequencies every now and then - this should be done with the event scheduler
134 static int i = 0; // Very ugly - but there should only ever be one instance of FGATCMgr.
136 //cout << "About to search navcomm1" << endl;
137 FreqSearch("comm", 0);
138 FreqSearch("nav", 0);
141 //cout << "About to search navcomm2" << endl;
142 FreqSearch("comm", 1);
143 FreqSearch("nav", 1);
148 //cout << "comm1 type = " << comm_type[0] << '\n';
149 //cout << "Leaving update..." << endl;
153 // Returns frequency in KHz - should I alter this to return in MHz?
154 unsigned short int FGATCMgr::GetFrequency(const string& ident, const atc_type& tp) {
156 bool ok = current_commlist->FindByCode(ident, test, tp);
157 return(ok ? test.freq : 0);
160 // Register the fact that the comm radio is tuned to an airport
161 // Channel is zero based
162 bool FGATCMgr::CommRegisterAirport(const string& ident, int chan, const atc_type& tp) {
163 SG_LOG(SG_ATC, SG_BULK, "Comm channel " << chan << " registered airport " << ident);
164 //cout << "Comm channel " << chan << " registered airport " << ident << ' ' << tp << '\n';
165 if(airport_atc_map.find(ident) != airport_atc_map.end()) {
166 if(tp == ATIS || tp == AWOS) {
167 airport_atc_map[ident]->atis_active = true;
171 //cout << "NOT IN MAP - creating new..." << endl;
172 const FGAirport *ap = fgFindAirportID(ident);
174 AirportATC *a = new AirportATC;
175 // I'm not entirely sure that this AirportATC structure business is actually needed - it just duplicates what we can find out anyway!
176 a->geod = ap->geod();
177 a->atis_freq = GetFrequency(ident, ATIS)
178 || GetFrequency(ident, AWOS);
179 a->atis_active = false;
180 if(tp == ATIS || tp == AWOS) {
181 a->atis_active = true;
183 airport_atc_map[ident] = a;
190 typedef map<string,int> MSI;
192 void FGATCMgr::ZapOtherService(const string ncunit, const string svc_name){
193 for (atc_list_iterator svc = atc_list->begin(); svc != atc_list->end(); svc++) {
195 if (svc->first != svc_name) {
196 MSI &actv = svc->second->active_on;
197 // OK, we have found some OTHER service;
198 // see if it is (was) active on our unit:
199 if (!actv.count(ncunit)) continue;
200 //cout << "Eradicating '" << svc->first << "' from: " << ncunit << endl;
203 //cout << "Eradicating service: '" << svc->first << "'" << endl;
204 svc->second->SetNoDisplay();
205 svc->second->Update(0); // one last update
206 SG_LOG(SG_GENERAL, SG_INFO, "would have erased ATC service:" << svc->second->get_name()<< "/"
207 << svc->second->get_ident());
208 // delete svc->second;
209 atc_list->erase(svc);
210 // ALL pointers into the ATC list are now invalid,
211 // so let's reset them:
212 atc_list_itr = atc_list->begin();
214 break; // cannot be duplicates in the active list
219 // Find in list - return a currently active ATC pointer given ICAO code and type
220 // Return NULL if the given service is not in the list
221 // - *** THE CALLING FUNCTION MUST CHECK FOR THIS ***
222 FGATC* FGATCMgr::FindInList(const string& id, const atc_type& tp) {
223 string ndx = id + decimalNumeral(tp);
224 if (!atc_list->count(ndx)) return 0;
225 return (*atc_list)[ndx];
228 // Returns true if the airport is found in the map
229 bool FGATCMgr::GetAirportATCDetails(const string& icao, AirportATC* a) {
230 if(airport_atc_map.find(icao) != airport_atc_map.end()) {
231 *a = *airport_atc_map[icao];
238 // Return a pointer to an appropriate voice for a given type of ATC
239 // creating the voice if necessary - ie. make sure exactly one copy
240 // of every voice in use exists in memory.
242 // TODO - in the future this will get more complex and dole out country/airport
243 // specific voices, and possible make sure that the same voice doesn't get used
244 // at different airports in quick succession if a large enough selection are available.
245 FGATCVoice* FGATCMgr::GetVoicePointer(const atc_type& type) {
246 // TODO - implement me better - maintain a list of loaded voices and other voices!!
249 case ATIS: case AWOS:
250 #ifdef ENABLE_AUDIO_SUPPORT
251 // Delayed loading fo all available voices, needed because the
252 // soundmanager might not be initialized (at all) at this point.
253 // For now we'll do one hardwired one
255 /* I've loaded the voice even if /sim/sound/pause is true
256 * since I know no way of forcing load of the voice if the user
257 * subsequently switches /sim/sound/audible to true.
258 * (which is the right thing to do -- CLO) :-)
260 if (!voiceOK && fgGetBool("/sim/sound/working")) {
263 voiceOK = v1->LoadVoice("default");
265 } catch ( sg_io_exception & e) {
267 SG_LOG(SG_ATC, SG_ALERT, "Unable to load default voice : "
268 << e.getFormattedMessage().c_str());
293 // Search for ATC stations by frequency
294 void FGATCMgr::FreqSearch(const string navcomm, const int unit) {
297 string ncunit = navcomm + "[" + decimalNumeral(unit) + "]";
298 string commbase = "/instrumentation/" + ncunit;
299 string commfreq = commbase + "/frequencies/selected-mhz";
300 SGPropertyNode_ptr comm_node = fgGetNode(commfreq.c_str(), false);
302 //cout << "FreqSearch: " << ncunit
303 // << " node: " << comm_node << endl;
304 if (!comm_node) return; // no such radio unit
307 double freq = comm_node->getDoubleValue();
308 _aircraftPos = SGGeod::fromDegFt(lon_node->getDoubleValue(),
309 lat_node->getDoubleValue(), elev_node->getDoubleValue());
311 // Query the data store and get the closest match if any
312 //cout << "Will FindByFreq: " << lat << " " << lon << " " << elev
313 // << " freq: " << freq << endl;
314 if(current_commlist->FindByFreq(_aircraftPos, freq, &data)) {
315 //cout << "FoundByFreq: " << freq
316 // << " ident: " << data.ident
317 // << " type: " << data.type << " ***" << endl;
318 // We are in range of something.
321 // Get rid of any *other* service that was on this radio unit:
322 string svc_name = data.ident+decimalNumeral(data.type);
323 ZapOtherService(ncunit, svc_name);
324 // See if the service already exists, possibly connected to
325 // some other radio unit:
326 if (atc_list->count(svc_name)) {
327 // make sure the service knows it's tuned on this radio:
328 FGATC* svc = (*atc_list)[svc_name];
329 svc->active_on[ncunit] = 1;
334 CommRegisterAirport(data.ident, unit, data.type);
336 // This was a switch-case statement but the compiler didn't like
337 // the new variable creation with it.
338 if(data.type == ATIS || data.type == AWOS) {
339 (*atc_list)[svc_name] = new FGATIS;
340 FGATC* svc = (*atc_list)[svc_name];
343 svc->active_on[ncunit] = 1;
349 // No services in range. Zap any service on this unit.
350 ZapOtherService(ncunit, "x x x");