]> git.mxchange.org Git - flightgear.git/blob - src/FDM/fdm_shell.cxx
Fix for CVE-2012-2091:
[flightgear.git] / src / FDM / fdm_shell.cxx
1 // fdm_shell.cxx -- encapsulate FDM implementations as well-behaved subsystems
2 //
3 // Written by James Turner, started June 2010.
4 //
5 // Copyright (C) 2010  Curtis L. Olson  - http://www.flightgear.org/~curt
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 // $Id$
22
23 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26
27 #include <cassert>
28 #include <simgear/structure/exception.hxx>
29
30 #include <FDM/fdm_shell.hxx>
31 #include <FDM/flight.hxx>
32 #include <Aircraft/replay.hxx>
33 #include <Main/globals.hxx>
34 #include <Main/fg_props.hxx>
35 #include <Scenery/scenery.hxx>
36
37 // all the FDMs, since we are the factory method
38 #ifdef ENABLE_SP_FDM
39 #include <FDM/SP/ADA.hxx>
40 #include <FDM/SP/ACMS.hxx>
41 #include <FDM/SP/MagicCarpet.hxx>
42 #include <FDM/SP/Balloon.h>
43 #endif
44 #include <FDM/ExternalNet/ExternalNet.hxx>
45 #include <FDM/ExternalPipe/ExternalPipe.hxx>
46
47 #ifdef ENABLE_JSBSIM
48 #include <FDM/JSBSim/JSBSim.hxx>
49 #endif
50
51 #ifdef ENABLE_LARCSIM
52 #include <FDM/LaRCsim/LaRCsim.hxx>
53 #endif
54
55 #include <FDM/UFO.hxx>
56 #include <FDM/NullFDM.hxx>
57
58 #ifdef ENABLE_YASIM
59 #include <FDM/YASim/YASim.hxx>
60 #endif
61
62 FDMShell::FDMShell() :
63   _tankProperties( fgGetNode("/consumables/fuel", true) ),
64   _impl(NULL),
65   _dataLogging(false)
66 {
67 }
68
69 FDMShell::~FDMShell()
70 {
71   delete _impl;
72 }
73
74 void FDMShell::init()
75 {
76   _props = globals->get_props();
77   fgSetBool("/sim/fdm-initialized", false);
78
79   _wind_north       = _props->getNode("environment/wind-from-north-fps",    true);
80   _wind_east        = _props->getNode("environment/wind-from-east-fps",     true);
81   _wind_down        = _props->getNode("environment/wind-from-down-fps",     true);
82   _control_fdm_atmo = _props->getNode("environment/params/control-fdm-atmosphere", true);
83   _temp_degc        = _props->getNode("environment/temperature-degc",       true);
84   _pressure_inhg    = _props->getNode("environment/pressure-inhg",          true);
85   _density_slugft   = _props->getNode("environment/density-slugft3",        true);
86   _data_logging     = _props->getNode("/sim/temp/fdm-data-logging",         true);
87   _replay_master    = _props->getNode("/sim/freeze/replay-state",           true);
88
89   createImplementation();
90 }
91
92 void FDMShell::reinit()
93 {
94   if (_impl) {
95     fgSetBool("/sim/fdm-initialized", false);
96     _impl->unbind();
97     delete _impl;
98     _impl = NULL;
99   }
100   
101   init();
102 }
103
104 void FDMShell::bind()
105 {
106   _tankProperties.bind();
107   if (_impl && _impl->get_inited()) {
108     if (_impl->get_bound()) {
109       throw sg_exception("FDMShell::bind of bound FGInterface impl");
110     }
111     _impl->bind();
112   }
113 }
114
115 void FDMShell::unbind()
116 {
117   if( _impl ) _impl->unbind();
118   _tankProperties.unbind();
119 }
120
121 void FDMShell::update(double dt)
122 {
123   if (!_impl) {
124     return;
125   }
126   
127   if (!_impl->get_inited()) {
128     // Check for scenery around the aircraft.
129     double lon = fgGetDouble("/sim/presets/longitude-deg");
130     double lat = fgGetDouble("/sim/presets/latitude-deg");
131         
132     double range = 1000.0; // in meters
133     SGGeod geod = SGGeod::fromDeg(lon, lat);
134     if (globals->get_scenery()->scenery_available(geod, range)) {
135         SG_LOG(SG_FLIGHT, SG_INFO, "Scenery loaded, will init FDM");
136         _impl->init();
137         if (_impl->get_bound()) {
138           _impl->unbind();
139         }
140         _impl->bind();
141         
142         fgSetBool("/sim/fdm-initialized", true);
143         fgSetBool("/sim/signals/fdm-initialized", true);
144     }
145   }
146
147   if (!_impl->get_inited()) {
148     return; // still waiting
149   }
150
151 // pull environmental data in, since the FDMs are lazy
152   _impl->set_Velocities_Local_Airmass(
153           _wind_north->getDoubleValue(),
154           _wind_east->getDoubleValue(),
155           _wind_down->getDoubleValue());
156
157   if (_control_fdm_atmo->getBoolValue()) {
158     // convert from Rankine to Celsius
159     double tempDegC = _temp_degc->getDoubleValue();
160     _impl->set_Static_temperature((9.0/5.0) * (tempDegC + 273.15));
161     
162     // convert from inHG to PSF
163     double pressureInHg = _pressure_inhg->getDoubleValue();
164     _impl->set_Static_pressure(pressureInHg * 70.726566);
165     // keep in slugs/ft^3
166     _impl->set_Density(_density_slugft->getDoubleValue());
167   }
168
169   bool doLog = _data_logging->getBoolValue();
170   if (doLog != _dataLogging) {
171     _dataLogging = doLog;
172     _impl->ToggleDataLogging(doLog);
173   }
174
175   switch(_replay_master->getIntValue())
176   {
177       case 0:
178           // normal FDM operation
179           _impl->update(dt);
180           break;
181       case 3:
182           // resume FDM operation at current replay position
183           _impl->reinit();
184           break;
185       default:
186           // replay is active
187           break;
188   }
189 }
190
191 FGInterface* FDMShell::getInterface() const
192 {
193     return _impl;
194 }
195
196 void FDMShell::createImplementation()
197 {
198   assert(!_impl);
199   
200   double dt = 1.0 / fgGetInt("/sim/model-hz");
201   string model = fgGetString("/sim/flight-model");
202
203   bool fdmUnavailable = false;
204
205   if ( model == "ufo" ) {
206     _impl = new FGUFO( dt );
207   } else if ( model == "external" ) {
208     // external is a synonym for "--fdm=null" and is
209     // maintained here for backwards compatibility
210     _impl = new FGNullFDM( dt );
211   } else if ( model.find("network") == 0 ) {
212     string host = "localhost";
213     int port1 = 5501;
214     int port2 = 5502;
215     int port3 = 5503;
216     string net_options = model.substr(8);
217     string::size_type begin, end;
218     begin = 0;
219     // host
220     end = net_options.find( ",", begin );
221     if ( end != string::npos ) {
222       host = net_options.substr(begin, end - begin);
223       begin = end + 1;
224     }
225     // port1
226     end = net_options.find( ",", begin );
227     if ( end != string::npos ) {
228       port1 = atoi( net_options.substr(begin, end - begin).c_str() );
229       begin = end + 1;
230     }
231     // port2
232     end = net_options.find( ",", begin );
233     if ( end != string::npos ) {
234       port2 = atoi( net_options.substr(begin, end - begin).c_str() );
235       begin = end + 1;
236     }
237     // port3
238     end = net_options.find( ",", begin );
239     if ( end != string::npos ) {
240       port3 = atoi( net_options.substr(begin, end - begin).c_str() );
241       begin = end + 1;
242     }
243     _impl = new FGExternalNet( dt, host, port1, port2, port3 );
244   } else if ( model.find("pipe") == 0 ) {
245     // /* old */ string pipe_path = model.substr(5);
246     // /* old */ _impl = new FGExternalPipe( dt, pipe_path );
247     string pipe_path = "";
248     string pipe_protocol = "";
249     string pipe_options = model.substr(5);
250     string::size_type begin, end;
251     begin = 0;
252     // pipe file path
253     end = pipe_options.find( ",", begin );
254     if ( end != string::npos ) {
255       pipe_path = pipe_options.substr(begin, end - begin);
256       begin = end + 1;
257     }
258     // protocol (last option)
259     pipe_protocol = pipe_options.substr(begin);
260     _impl = new FGExternalPipe( dt, pipe_path, pipe_protocol );
261   } else if ( model == "null" ) {
262     _impl = new FGNullFDM( dt );
263   }
264     else if ( model == "larcsim" ) {
265 #ifdef ENABLE_LARCSIM
266         _impl = new FGLaRCsim( dt );
267 #else
268         fdmUnavailable = true;
269 #endif
270     }
271     else if ( model == "jsb" ) {
272 #ifdef ENABLE_JSBSIM
273         _impl = new FGJSBsim( dt );
274 #else
275         fdmUnavailable = true;
276 #endif
277     }
278 #ifdef ENABLE_SP_FDM
279     else if ( model == "ada" ) {
280         _impl = new FGADA( dt );
281     } else if ( model == "acms" ) {
282         _impl = new FGACMS( dt );
283     } else if ( model == "balloon" ) {
284         _impl = new FGBalloonSim( dt );
285     } else if ( model == "magic" ) {
286         _impl = new FGMagicCarpet( dt );
287     }
288 #else
289     else if (( model == "ada" )||(model == "acms")||( model == "balloon" )||( model == "magic" ))
290     {
291         fdmUnavailable = true;
292     }
293 #endif
294     else if ( model == "yasim" ) {
295 #ifdef ENABLE_YASIM
296         _impl = new YASim( dt );
297 #else
298         fdmUnavailable = true;
299 #endif
300     } else {
301         throw sg_exception(string("Unrecognized flight model '") + model
302                + "', cannot init flight dynamics model.");
303     }
304
305     if (fdmUnavailable)
306     {
307         // FDM type is known, but its support was disabled at compile-time.
308         throw sg_exception(string("Support for flight model '") + model
309                 + ("' is not available with this binary (deprecated/disabled).\n"
310                    "If you still need it, please rebuild FlightGear and enable its support."));
311     }
312 }